次の方法で共有


Declaration of Variables

Applies To: Microsoft Dynamics AX 2012 R3, Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynamics AX 2012

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.

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;

Summary

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

Declaration

=

Datatype Variable { , Variable } ;

Datatype

=

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

Variable

=

[ Typeoptions ] Identifier [ Option ]

Typeoptions

=

[ Length ] [ left | right ]

Option

=

Arrayoption | Initialization

Arrayoption

=

[ Length , Memory ]

Initialization

=

expression

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

Announcements: New book: "Inside Microsoft Dynamics AX 2012 R3" now available. Get your copy at the MS Press Store.