Usina BRAdvPL Guide
Sign in
← All topics
FOUNDATIONPublished

Macro substitution

&cExpressao

Evaluates an AdvPL expression stored as text at runtime.

OperatorsExpressionsVariables
01 · OVERVIEW

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.

02 · SYNTAX

Syntax

&cExpressao
03 · PRACTICAL EXAMPLE

Evaluating a formula

User Function ExMacro()
    Private nValue := 10
    Private cFormula := "nValue + 1"
    Local nResult := &cFormula

    ConOut(nResult) // 11
Return
Expected result

cFormula is evaluated at runtime and produces the numeric value 11.

04 · PRACTICAL EXAMPLE

Dynamic field reference

User Function ExField()
    Local cField := "A1_NOME"

    DbSelectArea("SA1")
    ConOut(SA1->&cField)
Return
Expected result

The value of cField determines which SA1 field is read at runtime.

BEST PRACTICES
  • 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.
COMMON PITFALLS
  • 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.

Related content

REFERENCES
  1. Waterloo Ferreira da Silva. The AdvPL Bible. 2004, pp. 31–32. Summarized and adapted.
  2. TOTVS. SDK Microsiga Protheus. Versão 1.0, 31 jul. 2014 — seção O Operador Macro (&).
Status
Published
Page created on
Last reviewed on
Original language
Portuguese
Reviewed by
Usina.BR
0 approved comment(s)

Comments

There are no approved comments yet.

Sign in with Google or Microsoft to comment.

Powered by Usina Docs · Alpha