Code Indirection


Code indirection refers to the situation when you want to execute an Xbasic command that is contained in a variable (i.e. to indirectly execute an Xbasic command).

The EVALUATE_TEMPLATE( Command_String ) function is used to execute an Xbasic command that is contained in Command_String.

Alpha Anywhere also offers the EVALUATE_TEMPLATE_PRIVATE() command which executes the code in another session, rather than in the current session.

For example, assume that the variable, command, contains " dim global newVar as C ". The command: EVALUATE_TEMPLATE(command) would be the same as executing the command: dim global newVar as C.

The following example shows a function that takes a table name and a record number and creates global variables for each of the fields in the table, assigning the value in each field to the corresponding variable.

For example, if the table had three fields, Firstname, Lastname and Age, with corresponding field values of John, Smith and 34, the function would create three global variables called Firstname, Lastname and Age and assign the corresponding values to these variables.

Legacy: If you are familiar with Alpha Four, this example is equivalent to the XLOOKUPR() function.
function xlookup as L(tablename as C, rec as N)
    t = table.open(tablename)
    num_fields = t.fields_get()
    for I = 1 to num_fields
        f = t.field_get(I)
        evaluate_template("dim global " + f.name_get()+ " as " + f.type_get())
        eval(f.name_get()) = f.value_get()
    next I
end function

See Also