Name
Eval Function
Syntax
[result= ]Eval(expression)
resultUse: Optional
Data Type: Any
A variable to hold the result of the
Evalfunction.expressionUse: Required
Data Type: String
The expression to be evaluated.
Return Value
Any
Description
Evaluates an expression and returns the results.
Rules at a Glance
Eval follows the rules of precedence in evaluating
expression.If an equals sign (=) occurs in
expression, it is interpreted as a comparison operator rather than as an assignment operator. In this case, Eval returnsTrueif 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, Result
Test1 = 4
Test2 = 5
Result = Eval("Test1 = Test2")
MsgBox Result
Result = Eval("Test1 < Test2")
MsgBox Result
Result = Eval("Test1 / Test2")
MsgBox Result
Result = Eval("Test1 - Test2")
MsgBox ResultProgramming Tips and Gotchas
You may wonder why you’d want to bother with Eval when 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 Eval is that it evaluates expressions stored to strings. For example, the code:
Dim sExp, result, a, b, c a = 10 b = 20 c = 30 sExp = "a + b + c" result = eval(sExp)
returns 60. This means that you can build expressions and assign them to strings ...
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