ABL lets you define program variables for use within an application. A variable must be defined with a specific data type. A variable can be initialized with a value when it is defined, or it can be assigned a value later. You use the VAR statement or the DEFINE VARIABLE statement to define variables. The VAR statement is a newer shorthand notation for the DEFINE VARIABLE statement that is less verbose. Most variables can be defined using the VAR statement.

Define a variable using the VAR statement

The basic simplified syntax of the VAR statement is:

VAR datatype varname [ = expression[, expression ]...].
datatype
Specifies the data type of the variable, for example, CHAR, INT, DECIMAL, DATE, LOGICAL. See the following table for a list of supported data types.
varname
Specifies the name of the variable.
expression
An Expression which evaluates to a data type that is consistent with the data type of the defined variable. See the Default initial values table below for a list of default initial values for each data type.

The following example code shows some variables definitions using VAR:

/* CHARACTER variable with an initial value */
VAR CHAR s1 = "abc".

/* INTEGER variable */
VAR INT count.

/* DATE variable with an initial value */
VAR DATE d1 = 1/1/2020.
 
/* LOGICAL (boolean) variable */
VAR LOGICAL isPaidFor.

/* CHARACTER array with three elements */
VAR CHAR[3] s2.

/* INTEGER array with two elements */
VAR INT[2] z = [100, 200].

/* Instantiated object variable */
VAR myclass myobj = NEW myclass("Progress", 2021, ?).

/* Determinate array object variable */
VAR StateClass[2] objArrayA = [NEW StateClass(“MA”), NEW StateClass(“NH”)].
 
/* Indeterminate array object variable */
VAR StateClass[] objArrayB = [NEW StateClass(“MA”), NEW StateClass(“NH”)].

/* Object variable using expressions as parameters */
VAR point p1 = NEW point(x - 5, y + 5).

Define a variable using the DEFINE VARIABLE statement

The basic simplified syntax of the DEFINE VARIABLE statement is:

DEFINE VARIABLE varname AS datatype
  [ INITIAL { value } ]
  [ EXTENT [ n ] ]
  [ NO-UNDO ].
varname
Specifies the name of the variable.
datatype
Specifies the data type of the variable, for example, CHARACTER, INTEGER, DECIMAL, DATE, LOGICAL. See the following table for a list of supported data types.
INITIAL value
Optionally specifies an initial value for the variable. If specified, the value must be a constant. See the following table for a list of default initial values for each data type.
EXTENT n
Optionally specifies that the variable is an array of size n. The size must be an integer. After the array is defined, you can then reference the individual array elements in your code by enclosing the array subscript in brackets, as in myVar[2] = 5.
NO-UNDO
Specifies that the variable does not require transaction support. The best practice is to always use NO-UNDO for variables unless explicitly required, to prevent unnecessary overhead.

See Other variable qualifiers in Develop ABL Applications for more information on other qualifiers.

Data types

ABL supports a range of Data types, and each data type has a default initial value that can be overridden. The ABL data types are listed in the following table.

Table 1. Default initial values
Data type name Default initial value
CHARACTER or CHAR "" (The empty string)
CLASS object-type-name Unknown value (?)
DATE Unknown value (?)
DATETIME Unknown value (?)
DATETIME-TZ Unknown value (?)
DECIMAL 0
HANDLE Unknown value (?)
INTEGER or INT 0
INT64 0
LOGICAL no
LONGCHAR Unknown value (?)
MEMPTR Unknown value (?)
RAW A zero-length sequence of bytes
ROWID Unknown value (?)

Assign a value to a variable

ABL assigns values to variables using the assignment statement (=). In the following example, the variable, name, is set to the string, NewCustomer.

/* Set a local string variable to "NewCustomer" */
name = "NewCustomer".

See Assign a value to a variable for more information.

Compound assignment operators

ABL has compound assignment operators (+=, -=, *=, /=), which allow you to combine addition, subtraction, multiplication, and division operators with an assignment. See Compound assignment operators for more information.

ABL Unknown value

For certain data types, when a value is not yet assigned to the data element, ABL associates the Unknown value with the data element. In ABL, you represent the Unknown value with a question mark (?). Note that you do not put quotation marks around the question mark; the question mark acts as a special symbol on its own. The Unknown value (?) is not equal to any defined value. In ABL, you write a statement that looks as though you are comparing a value to a question mark, such as: IF ShipDate = ?, but the statement is asking if ShipDate has the Unknown value, which means it has no particular value. The Unknown value is like the NULL value in SQL, and the expression IF ShipDate = ? is like the SQL expression IF ShipDate IS NULL.

For more information, see Use the ABL Unknown value.