Using Scripts (JavaScript, VB Script) from C#

[Back] [Main] [Try Script.NET instead!]

We will use MS Script Control. First add reference:

code.GIF

2. Try this code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Reflection;

namespace WindowsApplication1
{
  [ComVisible(true)]
  public partial class Form1 : Form
  {
    public Form1()
    {
     InitializeComponent();

     MSScriptControl.ScriptControlClass scc = new MSScriptControl.ScriptControlClass();
     scc.Language = "vbscript";
     scc.AddCode("Function GetSqr(x)\n"+
       " GetSqr = Sqr(x)\n "+
       "End Function");
     scc.AddObject("external", this, true);

     //Call to C# code from Script
     scc.ExecuteStatement("external.ShowMe( GetSqr(16) )");
    }

    public void ShowMe(double val)
    {
     MessageBox.Show(val.ToString());
    }
  }
}

To debug this code you can use Script Debugger
    

Last updated: 28 may 2007