Code Indirection
Description
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.
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.
function xlookup as L(tablename as C, rec as N) t = table.open(tablename) t.index_primary_put() t.fetch_goto(rec) 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 t.close() end function
See Also