Usina BRAdvPL Guide
Sign in
← All topics
FOUNDATIONPublished

AdvPL variable types

Local <variável> := <valor> · ValType(<variável>)

Learn how to create, name, initialize, and assign AdvPL variables and how their type follows the stored value.

VariablesData typesAssignmentNILValTypeFoundations
01 · OVERVIEW

Overview

A variable is a named reference to a value held in memory during execution. In traditional AdvPL, its type is dynamic: it is determined by the assigned value and may change during the variable lifetime. This differs from scope, which controls visibility and lifetime, and explicit As <type> typing, which documents and constrains the expected contract.

02 · SYNTAX

Syntax

Local <variável> := <valor> · ValType(<variável>)

Parameters

Character
COptional

Text delimited by single or double quotes.

Numeric
NOptional

Integer or decimal floating-point number.

Date
DOptional

A date created by Date(), CToD(), SToD(), or another date function.

Logical
LOptional

True (.T.) or false (.F.).

Array
AOptional

A one-based indexed collection that may contain mixed types.

CodeBlock
BOptional

A set of expressions that can later be run with Eval().

Object
OOptional

An instance of a class.

NIL
UOptional

Absence of a value; the usual initial state of an uninitialized declared variable.

03 · PRACTICAL EXAMPLE

Declaration and initialization

User Function ExVariables()
    Local cProduct  := "Keyboard"
    Local nQuantity := 2
    Local nPrice    := 150.50
    Local lAvailable := .T.
    Local dCreated  := Date()

    If lAvailable
        MsgInfo(cProduct + " - total: " + ;
            CValToChar(nQuantity * nPrice))
    EndIf
Return
Expected result

Each variable receives an initial value that matches the intent communicated by its prefix.

04 · PRACTICAL EXAMPLE

NIL before initialization

User Function ExNil()
    Local nPercentage
    Local nResult

    If ValType(nPercentage) == "U"
        nPercentage := 10
    EndIf

    nResult := 250 * (1 + nPercentage / 100)
    MsgInfo("Result: " + CValToChar(nResult))
Return
Expected result

ValType() returns U while the variable has no value; initialization prevents NIL from entering the calculation.

05 · PRACTICAL EXAMPLE

Dynamic type change

User Function ExTipoDinamico()
    Local xValor := "Pedido aberto"

    ConOut("Tipo atual: " + ValType(xValor)) // C
    xValor := 22
    ConOut("Tipo atual: " + ValType(xValor)) // N
    xValor := .T.
    ConOut("Tipo atual: " + ValType(xValor)) // L
    xValor := Date()
    ConOut("Tipo atual: " + ValType(xValor)) // D
Return
Expected result

The same variable holds different types. This flexibility should be intentional.

06 · PRACTICAL EXAMPLE

Conversion before concatenation

User Function ExConversao()
    Local nQuantidade := 3
    Local dHoje       := Date()
    Local lAtivo      := .T.

    ConOut("Quantidade: " + CValToChar(nQuantidade))
    ConOut("Data: " + DToC(dHoje))
    ConOut("Ativo: " + IIf(lAtivo, "sim", "não"))
Return
Expected result

Each value is converted to character before it is joined to text.

07 · PRACTICAL EXAMPLE

Implicit assignment to avoid

User Function ExErroDigitacao()
    Local nQuantidade := 10

    nQuantidad := 20 // nome incorreto: pode criar uma Private

    MsgInfo("Quantidade: " + CValToChar(nQuantidade))
Return
Expected result

A missing letter may create another identifier rather than update nQuantity.

BEST PRACTICES
  • Declare and initialize in the same statement when a clear initial value exists.
  • Prefer := for assignment so it is visually distinct from = and == comparisons.
  • ValType(xValue) returns a letter for the current type and may change after another assignment.
  • Use consistent c, n, d, l, a, b, o, and x prefixes to communicate intent.
  • Consulted documentation records a ten-character identifier rule. Treat it as historical compatibility behavior and verify it on the AppServer version in use.
  • Keep concepts separate: type describes the value; scope controls visibility; lifetime determines how long the variable exists.
COMMON PITFALLS
  • An uninitialized declaration produces NIL, which may cause type mismatch in calculations or concatenation.
  • Assigning to an undeclared name may implicitly create a Private variable, so a typo can silently change program logic.
  • Changing types freely reduces predictability. Do it only intentionally and validate with ValType().
  • Convert numbers, dates, logical values, and NIL before concatenating them with text.
  • Do not use reserved words, spaces, or special characters in identifiers.
  • Store is retained for historical compatibility; prefer explicit declarations and := in new code.

Related content

REFERENCES
  1. TOTVS. Creating and Assigning Variables. TDN.
  2. TOTVS. Data Types. TDN.
  3. TOTVS. Variable Context within a Program. TDN.
  4. MASTERSIGA CONSULTORIA. Creating and Assigning Variables. Updated May 18, 2023.
  5. SILVA, Waterloo Ferreira da. The AdvPL Bible. 2004, pp. 22-24.
Status
Published
Page created on
Last reviewed on
Original language
Portuguese
Reviewed by
Usina.BR