Eval

Compiles and evaluates a twinBASIC expression supplied as a string, returning the result as a Variant.

Syntax: Eval( Expression ) As Variant

Expression
required String. A twinBASIC expression that resolves to a value — for example, "2 + 2", "Sqr(2)", or "UCase(""hello"")".

A fresh TbExpressionService is built for every call, with the standard library binder registered so the standard runtime functions (Sin, Sqr, Len, CStr, and the rest) are visible. The expression is then compiled and evaluated once, and the service is discarded.

For repeated evaluation of the same source, or for expressions that need to see application objects, construct the service explicitly and reuse a compiled ITbExpression.

Note BETA 1034

Eval raises -2147467259 for any error inside the expression: the error’s own number and description are lost. Eval("1 / 0") raises -2147467259, not 11.

Note BETA 1034

Eval also raises -2147467259 for an expression whose value is an Error value, such as Eval("CVErr(5)"), and for Len of Null, which outside an expression returns Null.

Note BETA 1034

In a built program, the first error the expression service raises has the description Automation error, and every later one Unspecified error. In a run from the IDE, every one has the description Automation error.

Common Errors

Error When
-2147467259 (&H80004005), Unspecified error Expression is a zero-length string or is not an expression, or it names something the standard library does not have
evaluating Expression raises an error, whatever its number: division by zero, an overflow, a type mismatch, an argument a function refuses
the value of Expression is an Error value, or Expression calls Len with Null
94 (&H5E), Invalid use of Null Expression is Null

Example

Debug.Print Eval("2 * (Sqr(2) + 1)")    ' 4.82842712474619
Debug.Print Eval("UCase(""hello"")")     ' "HELLO"

See Also