Skip to content

Eval

See Also: Miscellaneous Functions, String Functions, Get_StrictEval

Purpose

Eval returns the value of a string, evaluated as an expression at runtime.

Return Type

String

Syntax

(Eval({string-expression}))

What It Does

The eval function provides evaluation at runtime of expressions stored as strings.

Example 1

Procedure Test
    String sExpression sOperator
    Integer iOperand1 iOperand2 iResult

    Move 10 to iOperand1
    Move 8 to iOperand2
    Move "+" to sOperator
    Move (String(iOperand1) + sOperator + String(iOperand2)) to sExpression
    Move (Eval(sExpression)) to iResult
End_Procedure

In this example, the string stored in sExpression is parsed and evaluated, and the answer (18) is stored in the integer variable iResult.

Example 2

Procedure Test
    String sExpression sOperator sOperand1 sOperand2
    Number nResult

    Find Gt Invt.Recnum
    Move "Invt.Unit_Price" to sOperand1
    Move "Invt.On_Hand" to sOperand2
    Move "*" to sOperator
    Move (sOperand1 + sOperator + sOperand2) to sExpression
    Move (Eval(sExpression)) to nResult
    Send Info_Box ("The result of\n\n" + sExpression + "\n\nis " + String(nResult)) "Result"
End_Procedure

The result of the record value invt.unit_price multiplied by the value of invt.on_hand will be shown in the message box. For this sample, the file invt needs to be opened beforehand, and the Windows.pkg needs to be used.

Notes

  • Eval() should be used with care. Not all functions should or can be evaluated using Eval(). In particular, functions that are passed or return memory pointers (Address type) should not be used within an Eval(). For example, AddressOf() should not be used inside of an Eval() expression.

  • This function returns a value of type string. If the result of this function is moved to a variable of a type other than string, its value is converted to the destination type.