I want to evaluate a String with Boolean Values and Binary Logical Operators (And/Or).
When i write Dim Expression = ((True And False) Or True)
then the value of "Expression" Will be true.
However, when i write Dim Expression = "((True And False) Or True)"
and try to convert the string to Boolean using Convert.ToBoolean() like MsgBox(Convert.ToBoolean(Expression))
then i got an error stating: String was not recognized as a valid Boolean. as in the picture.
All previous posts deals with strings containing only "True" or "False". but rarely i found a post dealing with an Expression containing brackets and more than one operator as "((True Or False) And True)" or much more complicated expressions. is this supported in the convert.ToBoolean function and if yes why i am getting this error?
Asked
Active
Viewed 1.5k times
0

Mohammad M Moshawrab
- 21
- 1
- 1
- 5
-
You're asking it to convert the _string_ `"((True And False) Or True)"` directly into a boolean. Booleans can only be `True` or `False` and the only strings which match to those are `"True"` and `"False"`. It won't evaluate your string as if it was an actual piece of code and compute the result, it just treats it literally like a string, because that's what it is. And since it doesn't directly match a string version of a boolean, you get the error. – ADyson Feb 27 '18 at 10:53
-
It's clearly documented here: https://msdn.microsoft.com/en-us/library/86hw82a3(v=vs.110).aspx - "For a successful conversion to occur, the value parameter must equal either Boolean.TrueString, a constant whose value is True, Boolean.FalseString, a constant whose value is False, or it must be null". If you read the documentation it answers your question for you already. – ADyson Feb 27 '18 at 10:54
-
so isn't there a way to convert such a string into a Boolean Value? – Mohammad M Moshawrab Feb 27 '18 at 12:18
-
No because it's not a boolean, it's effectively a random string. Just because it looks like code which might evaluate to a boolean if it was code is not really relevant (because it's not code, it's a string). – ADyson Feb 27 '18 at 12:19
-
However, if you're looking for how to interpret an arbitrary string as code and execute it, this is a wider topic. The following may turn out to be of interest to you (just from a quick google search, you can search for more): http://www.nullskull.com/articles/20030908.asp, https://stackoverflow.com/questions/1511376/c-sharp-execute-a-string-as-code, https://stackoverflow.com/questions/4629/how-can-i-evaluate-c-sharp-code-dynamically – ADyson Feb 27 '18 at 12:22
1 Answers
1
There is a round-about way of evaluating an expression using Microsoft script control ..
Right click on your project and choose Add
In the sub menu, choose Reference
In the window that appears, on the left click on COM
Scroll down until you see MicrosoftScriptControl 1.0
and double click it.
Then click Ok
on the lower right of the window
This function takes a string and using VBScript, evaluates the string expression. You can of course tweak it to your needs.
Private Function EvaluateBoolean(formula As String) As Boolean
Dim sc As New MSScriptControl.ScriptControl
'SET LANGUAGE TO VBSCRIPT
sc.Language = "VBSCRIPT"
'ATTEMPT MATH
Try
Return Convert.ToBoolean(sc.Eval(formula))
Catch ex As Exception
'SHOW THAT IT WAS INVALID
MessageBox.Show("Invalid Boolean expression")
End Try
End Function

David Wilson
- 4,369
- 3
- 18
- 31