Data Type: Any
A variable to hold the result of the
Data Type: String
The expression to be evaluated.
Evaluates an expression and returns the results.
Eval follows the rules of precedence
If an equals sign (=) occurs in
expression, it is interpreted as a
comparison operator rather than as an assignment operator. In
this case, Eval returns
True if the parts of
expression are equal and
False if they are not.
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
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 Result
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 ...