473,405 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Cbool problem

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

Nov 21 '05 #1
12 2503
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.

--
Gerry O'Brien [MVP]
Visual Basic .NET(VB.NET)


"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

Nov 21 '05 #2
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

Nov 21 '05 #3
Maybe?

If mystring <> vbNullString Then
If CBool(InStr(mystring, "NO MATCH")) Then
MsgBox("FAIL")
Else
MsgBox("SUCCESS")
End If
End If

"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?


Nov 21 '05 #4
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.

--
Joe Fallon

"Imran Koradia" <no****@microsoft.com> wrote in message
news:#K**************@TK2MSFTNGP12.phx.gbl...
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


Nov 21 '05 #5
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!
--
Joe Fallon

"Gerry O'Brien [MVP]" <gerry dot obrien at gmail dot com> wrote in message
news:#O**************@TK2MSFTNGP15.phx.gbl...
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.

--
Gerry O'Brien [MVP]
Visual Basic .NET(VB.NET)


"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


Nov 21 '05 #6
"scorpion53061" <ad***@nospamherekjmsolutions.com> schrieb:
Maybe?

If mystring <> vbNullString Then


I think that 'vbNullString' is not the best choice here because of its
special semantics.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #7
"Joe Fallon" <jf******@nospamtwcny.rr.com> schrieb:
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>

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #8
Well I tried......
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:#4**************@tk2msftngp13.phx.gbl:
"scorpion53061" <ad***@nospamherekjmsolutions.com> schrieb:
Maybe?

If mystring <> vbNullString Then


I think that 'vbNullString' is not the best choice here because of its
special semantics.


Nov 21 '05 #9
"scorpion53061" <ad***@nospamherekjmsolutions.com> schrieb:
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'.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #10
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.

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:OB**************@TK2MSFTNGP10.phx.gbl...
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.

--
Joe Fallon

"Imran Koradia" <no****@microsoft.com> wrote in message
news:#K**************@TK2MSFTNGP12.phx.gbl...
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
>
>
>



Nov 21 '05 #11
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

--
Joe Fallon

"Imran Koradia" <no****@microsoft.com> wrote in message
news:eS**************@tk2msftngp13.phx.gbl...
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.

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:OB**************@TK2MSFTNGP10.phx.gbl...
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.
--
Joe Fallon

"Imran Koradia" <no****@microsoft.com> wrote in message
news:#K**************@TK2MSFTNGP12.phx.gbl...
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
>
>
>



Nov 21 '05 #12
My apologies, I did indeed misinterpret what you were doing.

Others have answered your question I see however.

--
Gerry O'Brien MCT, MCDBA, MCSD
Visual Developer .NET MVP
"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
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!
--
Joe Fallon

"Gerry O'Brien [MVP]" <gerry dot obrien at gmail dot com> wrote in message
news:#O**************@TK2MSFTNGP15.phx.gbl...
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.

--
Gerry O'Brien [MVP]
Visual Basic .NET(VB.NET)


"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
>
>
>



Nov 21 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bruce Davis | last post by:
I'm having a problem on windows (both 2000 and XP) with a multi-threaded tkinter gui application. The problem appears to be a deadlock condition when a child thread pops up a Pmw dialog window in...
11
by: Kostatus | last post by:
I have a virtual function in a base class, which is then overwritten by a function of the same name in a publically derived class. When I call the function using a pointer to the derived class...
0
by: Refky Wahib | last post by:
Hi I need Technical Support I finished a Great project using .Net and SQL Server and .Net Mobile Control My Business case is to implement this Program to accept about 1 Million concurrent...
9
by: Sudesh Sawant | last post by:
Hello, We have an application which communicates using remoting. There is a server which is a Windows Service. The server exposes an object which is a singleton. The client is a Web Application...
117
by: Peter Olcott | last post by:
www.halting-problem.com
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.