Declaration of Variables

All variables must be declared before they can be used. X++ does not allow variable declarations to be mixed with other X++ statements; variables must be declared before X++ statements.

You should separate variable declarations from the rest of your code with a semi-colon (;) on a separate line, at the end of the declarations:

int i;


// Other code

The syntax for the declaration of each variable type is described in the help topics for the Primitive Data Types and Composite Data Types.

When a variable is declared, memory is also allocated and the variable is initialized to the default value. The only exception to this is for objects, where you need to manually allocate memory by using the new method. For more information, see Classes as Data Types.

Declaration With Initialization

At times you might want a variable to have a value other than the default as soon as the variable is declared. X++ allows you to initialize variables in the declaration by adding an assignment statement:

// Assigns value of pi to 12 significant digits

real pi = 3.14159265359;

Another syntax is needed to initialize objects because they are initialized by invoking the new method on the class:

// Simple call to the new method in the Access class

Access accessObject = new Access();

Multiple Declarations

X++ allows you to declare more than one variable in the same declaration statement. For example:

// Declares 2 integers, i and j

int i,j;

// Declares array with 100 integers with 5 in memory and b as integer with value 1

int a[100,5], b=1;


The possible variable declarations in X++ are shown by using EBNF (Extended Backus Naur Form) grammar, in the following table:



Datatype Variable { , Variable } ;



boolean | int | real | date | str | container | typeidentifier



[ Typeoptions ] Identifier [ Option ]



[ Length ] [ left | right ]



Arrayoption | Initialization



[ Length , Memory ]




typeIdentifier can be a class, an extended data type, a table, a map, or an enumeration, which has been declared elsewhere. All of these are declared in the Application Object Tree.

Typeoptions are only valid for variables of type string.

Length is an integer (or expression) that specifies the maximum length of the string or array. If it is omitted, the string or array is dynamic.

Memory is an integer (or expression) that specifies how many array items should be held in memory. If it omitted, all items are held in memory.

See Also

Composite Data Types

Primitive Data Types

Variable Names

Variable Scopes