What you are looking for is dynamic code execution. Here's an example:
Private Sub DynamicExecutio n()
Dim strCode As String = " Imports System.Windows. Forms " _
& ControlChars.Cr Lf & "NameSpace myNameSpace" _
& ControlChars.Cr Lf & "Class class1" & _
ControlChars.Cr Lf & "public sub DynamicCode()" _
& ControlChars.Cr Lf & _
"messagebox.sho w(CStr(CBool(11 =10 OrElse 11=11)))" _
& ControlChars.Cr Lf & "end sub" & ControlChars.Cr Lf _
& "end class" & ControlChars.Cr Lf & "end NameSpace"
Dim VBProvider As New VBCodeProvider
Dim Compiler As ICodeCompiler = _
VBProvider.Crea teCompiler
Dim Parameters As New CompilerParamet ers
With Parameters.Refe rencedAssemblie s
.Add("System.dl l")
.Add("System.Wi ndows.Forms.dll ")
End With
Dim Results As CompilerResults
Results = Compiler.Compil eAssemblyFromSo urce( _
Parameters, strCode)
Dim oclass1 As Object = _
Results.Compile dAssembly.Creat eInstance( _
"myNameSpace.cl ass1", True)
oclass1.GetType .InvokeMember( _
"DynamicCod e", BindingFlags.In vokeMethod, _
Nothing, oclass1, New Object() {})
End Sub
here's a more detailed example:
http://www.west-wind.com/presentatio...ynamicCode.htm
hope that helps..
Imran.
"Joe Fallon" <jf******@nospa mtwcny.rr.com> wrote in message
news:uE******** *****@TK2MSFTNG P12.phx.gbl...
I built up a string in code and now I wish to evaluate it using Cbool but
I get a runtime error.
This is my expression:
?CBool("11=10 OrElse 11=11")
Run-time exception thrown : System.InvalidC astException - Cast from string
"11=10 OrElse 11=11" to type 'Boolean' is not valid.
If I remove the quotes manually from the string the expression works as I
had hoped:
?CBool(11=10 OrElse 11=11)
True
The question is: how do you build up the string programatically and then
evaluate it using Cbool?
--
Joe Fallon