The VAR statement is an ABL statement that allows developers to define program variables more concisely, in a manner similar to modern programming languages. The VAR statement is a short-hand notation for the DEFINE VARIABLE statement. With VAR, you always get NO-UNDO behavior, multiple variables can be defined in a single statement, bracket notation is used for defining arrays, and the equal sign (=) is used to assign initial values.

Syntax

VAR [ access-mode ] [ STATIC | SERIALIZABLE | NON-SERIALIZABLE ]
    type [ [ [ size ] ] ] | [ CLASS ] classname [ [ [ size ] ] ]  
    variable [ = expression [, expression ]...].

For more details about the syntax, see the VAR statement in the ABL Reference.

VAR statement features

The VAR statement:
  • Is NO-UNDO, meaning that the value of the variable will not be restored if the variable changed during a transaction and the transaction is undone. If you wish to have the capability to restore the variable to its prior value, use the DEFINE VARIABLE statement instead.
  • Allows you to define multiple variables in one statement. For example:
    VAR CHAR s1, s2, s3.
  • Allows you to define arrays without the EXTENT keyword, using bracket notation instead. If the brackets are empty ([]), the array is indeterminate. For example:
    VAR CHAR[10] s4. //s4 is a determinate array with 10 elements
    VAR CHAR[]   s5. //s5 is an indeterminate array
  • Allows you to specify initial values without the INITIAL keyword, using an equal sign (=) instead. For example:
    VAR CHAR s6 = "My character string".
  • Allows you to instantiate an object in a single statement. For example:
    VAR myclass myobj = NEW myclass().
  • Allows you to instantiate and initialize array object variables. For example:
    VAR StateClass[2] objArrayA = [NEW StateClass(“MA”), NEW StateClass(“NH”)].
    VAR StateClass[] objArrayB = [NEW StateClass(“MA”), NEW StateClass(“NH”)].
    
  • Allows you to use expressions. For example:
    VAR DATETIME dtm = DATETIME(TODAY,MTIME).

VAR statement restrictions

The VAR statement has the following restrictions.
  • VAR may only be used at the beginning of an ABL routine, before any executable statements in the block.
    If in a procedure, the VAR statement must follow these statements:
    • USING...
    • BLOCK-LEVEL ON ERROR UNDO, THROW.
    • ROUTINE-LEVEL ON ERROR UNDO, THROW.
    If in a class, the VAR statement:
    • Must come after the CLASS statement.
    • Can be in any order within the class, like other class members.
    • Must be at the top of the block within a method or property body.
  • VAR does not allow abbreviated data types. For example, when specifying a decimal variable, you cannot write VAR DEC. You must write VAR DECIMAL. However, CHAR and INT are actual keywords and not abbreviations. CHAR is a synonym for CHARACTER and INT is a synonym for INTEGER. Therefore you can write VAR CHAR or VAR INT.
  • Other options allowed with the DEFINE VARIABLE statement, such as FORMAT or LABEL, cannot be used with VAR. If you need those other options, use the DEFINE VARIABLE statement instead.

Examples

The following examples show some of the ways you can write VAR statements along with their equivalent DEFINE VARIABLE statements.

Four character variables with default initial values:

VAR CHAR s1, s2, s3, s4.
DEFINE VARIABLE s1 AS CHAR NO-UNDO.
DEFINE VARIABLE s2 AS CHAR NO-UNDO.
DEFINE VARIABLE s3 AS CHARACTER NO-UNDO.
DEFINE VARIABLE s4 AS CHAR NO-UNDO.

Three integer variables (z’s initial value is 3 and x and y default to 0):

VAR INT x, y, z = 3.
DEFINE VARIABLE x AS INT NO-UNDO.
DEFINE VARIABLE y AS INT NO-UNDO.
DEFINE VARIABLE z AS INT INITIAL 3 NO-UNDO.

Three object variables:

VAR mypackage.subdir.myclass myobj1, myobj2, myobj3.
DEFINE VARIABLE myobj1 AS CLASS mypackage.subdir.myclass NO-UNDO.
DEFINE VARIABLE myobj2 AS CLASS mypackage.subdir.myclass NO-UNDO.
DEFINE VARIABLE myobj3 AS CLASS mypackage.subdir.myclass NO-UNDO.

Object variable with the optional CLASS keyword:

VAR CLASS mypackage.subdir.myclass myobj1.
DEFINE VARIABLE myobj1 AS mypackage.subdir.myclass NO-UNDO.

.NET generic object:

VAR "System.Collections.Generic.List<char>" cList.
DEFINE VARIABLE cList AS “System.Collections.Generic.List<CHAR>” NO-UNDO.

Three date variables (d1's initial value defaults to the Unknown value (?)):

VAR DATE d1, d2 = 1/1/2020, d3 = TODAY.
DEFINE VARIABLE d1 AS date NO-UNDO.
DEFINE VARIABLE d2 AS date INITIAL 1/1/2020 NO-UNDO.
DEFINE VARIABLE d3 AS date INITIAL TODAY NO-UNDO.

Date variables defined with an access mode:

VAR PROTECTED DATE d1, d2 = 1/1/2020.
DEFINE PROTECTED VARIABLE d1 AS DATE NO-UNDO.
DEFINE PROTECTED VARIABLE d2 AS DATE INITIAL 1/1/2020 NO-UNDO.

Three determinate arrays of size 3 (x's third element defaults to 2 (the previous element); y's elements default to 0):

VAR INT[3] x = [1, 2], y, z = [100, 200, 300].
DEFINE VARIABLE x AS INT EXTENT 3 NO-UNDO INITIAL [1, 2].
DEFINE VARIABLE y AS INT EXTENT 3 NO-UNDO. 
DEFINE VARIABLE z AS INT EXTENT 3 NO-UNDO INITIAL [100, 200, 300].

Two indeterminate arrays:

VAR INT[] x, y = [1,2,3].
DEFINE VARIABLE x AS INT EXTENT NO-UNDO.
DEFINE VARIABLE y AS INT EXTENT NO-UNDO INITIAL [1,2,3].

Object array of size 2:

VAR foo[2] classArray.
DEFINE VARIABLE classArray AS CLASS foo EXTENT 2.

Instantiated object variable:

VAR myclass myobj = NEW myclass("Progress",2021,?).
DEFINE VARIABLE myobj AS myclass NO-UNDO.
myobj = NEW myclass("Progress",2021,?).

Instantiated and initialized determinate array object variable:

Note: Uninitialized elements in a determinate array object variable are set to the Unknown (?) value, unlike scalar array variables where uninitialized elements are set to the last initialized value, if any.
VAR StateClass[2] objArrayA = [NEW StateClass(“MA”), NEW StateClass(“NH”)].
DEFINE VARIABLE objArrayA AS StateClass EXTENT 2 NO-UNDO.
objArrayA[1] = NEW StateClass(“MA”).
objArrayA[2] = NEW StateClass(“NH”).
Instantiated and initialized indeterminate array object variable:
VAR StateClass[] objArrayB = [NEW StateClass(“MA”), NEW StateClass(“NH”)].
DEFINE VARIABLE objArrayB AS StateClass EXTENT NO-UNDO.
EXTENT (objArrayB) = 2.  // Set the size
objArrayB[1] = NEW StateClass(“MA”).
objArrayB[2] = NEW StateClass(“NH”).

Variable initialized using an expression:

VAR DATETIME dtm = DATETIME(TODAY,MTIME).
DEFINE VARIABLE dtm AS DATETIME NO-UNDO.
dtm = DATETIME(TODAY,MTIME).