Name
Eval Function
Syntax
[result = ]Eval(expression)
-
result Use: Optional
Data Subtype: Any
A variable to hold the result of the
Evalfunction.-
expression Use: Required
Data Subtype: String Expression
The expression to be evaluated.
Return Value
Any
Description
Evaluates an expression and returns the results.
Rules at a Glance
Evalfollows the rules of precedence in evaluatingexpression.expressioncan be a literal expression or a string variable containing the expression to be evaluated.If an equals sign (=) occurs in
expression, it is interpreted as a comparison operator rather than as an assignment operator. In this case,EvalreturnsTrueif the parts ofexpressionare equal andFalseif they are not.
Example
In this example, the first result will always evaluate to False,
since the variables are not equal, and the second will always
evaluate to True, since Test1 is in fact
less than Test2.
Dim Test1, Test2, varResults Test1 = 4 Test2 = 5 VarResults = Eval(Test1 = Test2) Msgbox varresults VarResults = Eval(Test1 < Test2) Msgbox VarResults VarResults = Eval(Test1 + Test2) Msgbox VarResults VarResults = Eval(Test1 - Test2) Msgbox VarResults
Programming Tips & Gotchas
You may wonder why you’d want to bother with
Evalwhen you can do the same thing without it. For example:lVar1 = 2 lVar2 = 3 lResult = lVar1 + lVar2
is the same as:
lVar1 = 2 lVar2 = 3 lResult = Eval(lVar1 + lVar2)
But the significance of
Evalis that it evaluates expressions stored to strings. For example, the code:Dim sExp, result, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access