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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Expression Evaluator for comparison statements

Status
Not open for further replies.

MindXFlayer

Programmer
Nov 6, 2001
3
US
I am looking for a class/API call that can take:
6 > 5 OR 7 > 5 AND (A = B)

and return either True or False.

I have found plenty of math expression evaluators and tried the EbExecuteLine (but it requires vba6.dll which is not always registered on a system) but have yet to find what I need.

Any pointers would be appreciated!!
 
What about Cbool() does that not help?

Just a thought
Scoty ::) "Learn from others' mistakes. You could not live long enough to make them all yourself."
-- Hyman George Rickover (1900-86),
 
I think you may find that this is a problem with operator precedence. Presumably, no matter what the values assigned to A and B, you are getting the answer True - which makes you feel that VB's built-in expression evaluator is somehow wrong. But precedence says that AND is evaluated before OR, which means that firstly 7 > 5 and (A = B) gets evaluated (which will result in True or False depending on A and B), and then 6 > 7 OR True/False gets evaluated, which will always give you True. So you need to use parenthesis. The following will, I suspect, allow VB to evaluate the expression as you expect it to:

(6 > 5 OR 7 > 5) AND (A = B)
 
Scoty, thanks for the reply. Unfortunately, Cbool can not take a string like "6 > 5". It takes "6" > "5", i.e. the comparison operator (>) can not be part of the string.

Strongm, thanks for the reply. The problem is that I need to send this as a string. So I am given the string "6 > 5" and would like to know if the string is true/false without parsing it. Something like eval does in Javascript. Mind Flayer
---
MindXFlayer@hotmail.com
 
Ah! Well, in that case you want to look at the MS Script Control. Just add a reference to it in your project, and then try out:
[tt]
Dim WSC As ScriptControl

Set WSC = New ScriptControl

WSC.Language = "vbscript"
MsgBox WSC.Eval("6 > 5")
Set WSC = Nothing
[/tt]
Or, if you want to do it without making a reference:
[tt]
Dim WSC As Object

Set WSC = CreateObject("MSScriptControl.ScriptControl") 'New ScriptControl

WSC.Language = "vbscript"
MsgBox WSC.Eval("6 > 5")
Set WSC = Nothing
 
Hmmmmmmm,

Something strange this way comes.

The Expression works "O.K." - if "A" and "B" are VARIABLES

as in:

A = 4
B = 5
? Eval("6 > 5 AND 7 > 5") AND (A = B)


It is ALSO "O.K." if "A" and "B" are (properly?) delimited as literals,

as in:

? Eval("6 > 5 AND 7 > 5 AND 'A' = 'B'")
0

So, despite much ado over the process, I think you need to be CAREFUL about what you ask for -after all- YOU JUST MIGHT get it (or even already have it?) and just not 'appreciate' it?


MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top