FOUNDATIONPublished
Macro substitution
&cExpressaoEvaluates an AdvPL expression stored as text at runtime.
Overview
The ampersand macro operator (&) makes a character expression be interpreted and evaluated during execution. It supports dynamic formulas and field references, but moves syntax validation to runtime and requires strict control over the evaluated text.
Syntax
&cExpressaoEvaluating a formula
User Function ExMacro()
Private nValue := 10
Private cFormula := "nValue + 1"
Local nResult := &cFormula
ConOut(nResult) // 11
ReturnExpected result
cFormula is evaluated at runtime and produces the numeric value 11.
Dynamic field reference
User Function ExField()
Local cField := "A1_NOME"
DbSelectArea("SA1")
ConOut(SA1->&cField)
ReturnExpected result
The value of cField determines which SA1 field is read at runtime.
- Use macro substitution only when the expression truly needs to be defined at runtime.
- Strictly validate text received from users or external sources.
- Prefer direct code when the expression is known during development.
- Historical documentation states that variables referenced inside the expression cannot be local; verify the behavior in the target release.
- Syntax errors inside the string appear only at runtime.
- Evaluating untrusted text may execute unexpected expressions.

Comments
There are no approved comments yet.
Sign in with Google or Microsoft to comment.