What you are looking for is dynamic code execution. Here's an example:
Private Sub DynamicExecution()
Dim strCode As String = " Imports System.Windows.Forms " _
& ControlChars.CrLf & "NameSpace myNameSpace" _
& ControlChars.CrLf & "Class class1" & _
ControlChars.CrLf & "public sub DynamicCode()" _
& ControlChars.CrLf & _
"messagebox.show(CStr(CBool(11=10 OrElse 11=11)))" _
& ControlChars.CrLf & "end sub" & ControlChars.CrLf _
& "end class" & ControlChars.CrLf & "end NameSpace"
Dim VBProvider As New VBCodeProvider
Dim Compiler As ICodeCompiler = _
VBProvider.CreateCompiler
Dim Parameters As New CompilerParameters
With Parameters.ReferencedAssemblies
.Add("System.dll")
.Add("System.Windows.Forms.dll")
End With
Dim Results As CompilerResults
Results = Compiler.CompileAssemblyFromSource( _
Parameters, strCode)
Dim oclass1 As Object = _
Results.CompiledAssembly.CreateInstance( _
"myNameSpace.class1", True)
oclass1.GetType.InvokeMember( _
"DynamicCode", BindingFlags.InvokeMethod, _
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******@nospamtwcny.rr.com> wrote in message
news:uE*************@TK2MSFTNGP12.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.InvalidCastException - 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