Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert String to Boolean 1

Status
Not open for further replies.

Sameal

Programmer
Aug 6, 2001
142
0
0
US
I have a string that like this.

Dim switchClause As String
switchClause = "1 = 0"

I want to then use the switchClause in an If statement like this.

If switchClause Then
' Do something cause it was true.
Else
' Do something else cause it was false.
End If

The above doesn't work so I found the Parse() method of the Boolean object. Unfortunately for me it only will accept the following four strings:
"0", "1", "False", "True"

All of which are useless to what I'm doing. I tried wrapping an additional set of " inside the string but this failed as well when used directly with the If statement. Is there any way at all to parse this string into a boolean as if it was actually in the code? Any help will be a life saver thanks!!!
 
How about this - it's just a simple function which splits the string expression using the equals sign and then compares the strings on either side.

Public Function blnExamineString(ByVal strInput As String) As Boolean
If InStr(strInput, "=") = 0 Then Exit Function
Dim strElements() As String = Split(strInput, "=")
Return Trim(strElements(0)) = Trim(strElements(1))
End Function

Does that help? Mark [openup]
 
Yes a little bit. I have already written a version to do this but it is primitive. The function I wrote accepts a string, then splits it into a string array and then does a case switch on the operand. It then performs and If using the first and third operands using the operand which matched in the case switch. The problem with this is that it only accepts very primitive boolean expressions. I was hoping to find something that could parse any valid boolean expression into a boolean. Such as...

"a > b && b = c || a = b"

Obviously the code to manually parse this is a bit more difficult but doable. I was hoping there would be some kind of general parse() function that would make the compiler think that what the String object contains is it's boolean expression.
 
Hello again.
I suspected that you were not after such a simple solution!

Well, if you don't mind using a COM component, you can place a Microsoft Script control (msscript.ocx) on your component, and then use the eval method as follows

MessageBox.Show(AxScriptControl1.Eval(TextBox1.Text))

But if you want to keep things strictly dot net, then I'm not sure. I've been after an answer for this myself for ages. It is supposed to be possible to evaluate string expressions, but I'm not sure how. One thing you might look at is saving the expression to a text file along with some vb wrapper code so that the file can be compiled on the fly as a module, using system.reflection
Mark [openup]
 
Thanks so much for the leads!

Still undecided on whether to go with the script object idea or just break down and build a boolean expression parser. I've got a good model from an algebraic expression parser in one of my college text books so I may end up going that direction. Thanks for everything.
 
The MSScriptControl.ScriptControl object rocks! I got it all working in three lines of code yepee...
 
Thought I'd help anyone coming here, here is the code to parse strings into booleans.

Dim clause As String
Dim switch As Boolean
Dim scripter As New MSScriptControl.ScriptControl()
scripter.Language = "VBScript"
clause = "1 = 0"
switch = scripter.Eval(clause)

If switch Then
' True Path
Else
' False Path
End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top