Cbool problem

  • Thread starter Thread starter Joe Fallon
  • Start date Start date
J

Joe Fallon

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?
 
11 and 10 are not string values as you have them here. You could try
"11"="10" OrElse "11"="11").

By removing the quotes, the values become integer literals which is what
they are by default.
 
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/presentations/dynamicCode/DynamicCode.htm

hope that helps..
Imran.
 
Maybe?

If mystring <> vbNullString Then
If CBool(InStr(mystring, "NO MATCH")) Then
MsgBox("FAIL")
Else
MsgBox("SUCCESS")
End If
End If
 
OK.
I am now trying to use dynamic code execution in an ASP.Net application.

The code works fine a WinForms app.


This is the error I am getting when trying to load the dynamic assembly:
Dim a As System.Reflection.Assembly = cr.CompiledAssembly




Any ideas what I need to do to get the web server to load the assembly?

=== Pre-bind state information ===
LOG: Where-ref bind. Location = C:\WINDOWS\TEMP\umkevkky.dll
LOG: Initial PrivatePath = bin
Calling assembly : (Unknown).
===

LOG: Policy not being applied to reference at this time (private, custom,
partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/WINDOWS/TEMP/umkevkky.dll.
 
Gerry,
I think we are talking about different things.
Let me try again.

I am looping over a collection of values and dynamically building a string.
The simple example I posted can be quite a bit longer but the idea is the
same.

When I am done building the string it looks like this: 11=10 OrElse 11=11
But since it is a string it is really like this: "11=10 OrElse 11=11"

I can't remove the quotes - they are not there.

Dim someString As String = "11=10 OrElse 11=11"
If CBool(someString) = True Then

The above line crashes.
It would work fine if someString was not really a string and I could pass
in:
11=10 OrElse 11=11
without any quotes around it.

But how do I dynamically build a string and then "remove the quotes"?

I am trying the dynamic code execution and got it working in Winforms but
ASP.Net is not loading it.
Drat!
 
scorpion53061 said:
Maybe?

If mystring <> vbNullString Then

I think that 'vbNullString' is not the best choice here because of its
special semantics.
 
Joe Fallon said:
I built up a string in code and now I wish to evaluate it
using Cbool but I get a runtime error.

'CBool' will evaluate an expression, but it cannot evaluate an expression in
form of a string.

There is no direct equivalent to JavaScript's 'eval'. 'eval' is common for
scripting languages that come with an interpreter. This makes it easy to
implement eval because the source parser/... has to be shipped with the
scripting engine. For compiled programming languages it's not that easy.

Runtime Compilation (A .NET eval statement)
<URL:http://www.codeproject.com/dotnet/evaluator.asp>
 
scorpion53061 said:
Well I tried......

I forgot to give a brief explanation: 'vbNullString' is not the same as ""
is, and it should only be used when calling external procedures, for example
a function declared using 'Declare'.
 
Joe,

Unfortunately I'm not familiar with ASP.NET. However, looking at the
message, I believe the problem is that the runtime is creating a temporary
assembly in the Temp folder and probably the ASP.NET account does not have
permissions to write to the directory. Thats just a guess..but yes, it does
look like a permissions issue to me.


Imran.
 
Imran,
I had already added Read permission to Network Service account on the temp
folder.

It turns out that message is returned every time there is a syntax error in
the code string being Evaluated.

Once I input a good sample string it ran correctly.

The other major piece was that in the Eval method there are Imports
statements in the code.
Any assembly that is imported needs to be added to the param collection.
Once I married them up and got a good sample it worked!

cp.ReferencedAssemblies.Add("system.dll")

sb.Append("Imports System" & vbCrLf)


Thanks for the help.

I used the code from this article: (thanks Peter!)
http://www.eggheadcafe.com/articles/20030908.asp
 
My apologies, I did indeed misinterpret what you were doing.

Others have answered your question I see however.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top