Udostępnij za pośrednictwem


Enums

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

X++ does not support constants but has an enumerable type (enum), which is a list of literals. You need to create an enum in the Application Object Tree (AOT) before you can use it.

Enum values are represented internally as integers. The first literal has number 0, the next has number 1, the next has number 2, and so on. You can use enums as integers in expressions.

There are hundreds of enumerable types that are built into the standard application. For example, the enum NoYes has two associated literals, where No has the value 0, and Yes has the value 1.

Tip

To get a list of all enums when you are using the X++ code editor, press F11.

Creating an Enum

To create an enum

  1. Expand the Data Dictionary node in the AOT.

  2. Right click the Base Enums node and select New Base Enum.

  3. Rename the enum.

  4. The literals in the enum are called elements. Right-click the enum and select New Element.

  5. Rename the element.

  6. Add as many additional elements as you need.

By default, elements take values in the order in which they are created. The first element has the value 0, the second element has the value 1, and so on. However, you can explicitly assign a particular integer value to an element by using the EnumValue property.

Range and Precision

You can create as many enums as you want, and you can declare up to 251 (0 to 250) literals in a single enum type.

Declaring Enums

You must create an enum type in the AOT before you can declare it.

Enum declaration

=

enumname Variable { , Variable } ;

Variable

=

identifier [ option ]

Option

=

Arrayoptions | initialization

    //A NoYes enum
    NoYes done;
     
    //An array of Criteria enums 
    Criteria crit[100];

Referencing Enums in X++

To reference an enum, use the name of the enum, followed by the name of the literal, separated by two colons:

EnumName::literal

For example, to use the literal No in the NoYes enum, write NoYes::No.

Automatic Conversion

Enums are automatically converted into Booleans, integers, and reals.

Overview of Enums

Keyword

None—the keyword is the name of the enum

Size of data type

Byte

Scope of data type

User-defined

Default value

0 (first literal)

Implicit conversions

Automatically converted to Boolean, int, and real

Explicit conversions

enum2str, str2enum

See also

Data Types in X++

Variables

enum2Str Function

str2Enum Function

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