When the Data became Code: combining JavaScript and C# code
Thursday, August 30th, 2007We will use MS Script Control.
First add reference:
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());
}
}
}