Compile
Parses an expression string and returns it as a compiled ITbExpression.
Syntax: service.Compile( expression )
- service
- required An object expression that evaluates to a TbExpressionService object.
- expression
- required A String containing a twinBASIC-syntax expression — for example,
"Sqr(2) + 1"or"UCase(FirstName) & "" "" & UCase(LastName)".
The return value is an ITbExpression. Calling Evaluate on it runs the expression and produces the current value; the same instance can be evaluated as many times as needed.
Symbols referenced in expression — function names, object members, properties — are resolved against the binders registered with service at the time of the call. Without a binder only an expression that names nothing compiles, such as "1 + 1"; the most common starting point is AddStdLibraryBinder, which exposes the standard runtime library.
Compilation is the relatively expensive step; evaluation reuses the compiled form. When a piece of source text will be used for repeated evaluation — a formula column refreshed every row, a watch expression sampled in a debugger — compile it once and keep the ITbExpression around.
An expression that compiles can still fail when it is evaluated: "1 / 0" compiles, and Evaluate raises the error.
Warning BETA 1042
Some malformed expressions are not refused: the part that cannot be read is left out, and no error is raised. "2 +", "2 Mod", "(2" and "2)" each compile to an expression whose value is 2.
Note BETA 1042
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 expression names something that no registered binder resolves (any name, when no binder is registered), or calls a function with the wrong number of arguments |
94 (&H5E), Invalid use of Null | expression is Null |
Example
Dim Service As TbExpressionService = New TbExpressionService
Service.AddStdLibraryBinder()
Dim Square As ITbExpression = Service.Compile("Sqr(2)")
Debug.Print Square.Evaluate() ' 1.4142135623731
Debug.Print Square.Evaluate() ' Same compiled instance, evaluated again.
See Also
- Evaluate method
- AddStdLibraryBinder method
- AddCustomBinderObject method
- AddCustomBinder method