Array in X++ Language - D365 F&O

Rumman Ansari   Software Engineer   2023-08-31   9212 Share
☰ Table of Contents

Table of Content:


Array in X++ Language

In X++, which is a programming language used in Microsoft Dynamics AX and its successor Dynamics 365 Finance and Operations, arrays are used to store collections of values of the same data type. Arrays provide a way to manage and manipulate multiple values in a structured manner.


There are three kinds of arrays:

  • Dynamic
  • Fixed-length
  • Partly on disk

Array Initialization Code



static void RummanJob1(Args _args)
{

    // array declaration example
    // by Rumman Ansari

        // A dynamic array of integers
        int arrayName[]; 
     
        // A fixed-length real array with 100 elements
        real arrayName1[100]; 
     
        // A dynamic array of dates with only 10 elements in memory
        date arrayName2[,10]; 
     
        // A fixed length array of NoYes variables with
        // 100 elements and 10 in memory
        NoYes arrayName3[100,10];
        
        print "Done.";
        pause; 
}



Array Indices

Array indexes begin at 1. The first item in the array is called [1], the second [2], and so on.

The syntax for accessing an array element is:

ArrayItemReference = ArrayVar [ Index ]

where ArrayVar is the identifier of the array, and Index is the number of the array element. Index can be an integer expression.

For example, a[7] accesses item number 7 in array a.

In X++, item zero[0] is used to clear the array! Assigning a value to index 0 in an array resets all elements in the array to the default value. For example,

intArray[0] = 0; //Resets all elements in intArray

Dynamic Arrays

A dynamic array is declared with an empty array option (that is, only square brackets):

//Dynamic array of integers
int intArrayName[];

// Dynamic array of variables of type Datatype

Datatype arrayVariableName[];

Fixed-length Arrays

A fixed-length array can hold the number of items that is specified in the declaration. Fixed-length arrays are declared like dynamic arrays but with a length option in the square brackets:

boolean boolArray[100]; //Fixed-length array of booleans with 100 items

Partly On Disk Arrays

Partly on disk arrays are declared either as dynamic or fixed-length arrays with an extra option that declares how many items should be held in memory. The other items are stored on disk and automatically loaded when referenced.

//Dynamic integer array with only 100 elements in memory.
int arrayVariableName [ ,100]; 
 
//Fixed-length string array with 800 elements, and only 100in memory.
str arrayVariableName [800, 100]

Fixed length Array - X++ Programming Language - D365 F&O

A fixed-length array can hold the number of items that is specified in the declaration. Fixed-length arrays are declared like dynamic arrays but with a length option in the square brackets:


 boolean boolArray[100]; //Fixed-length array of booleans with 100 items 

Example


internal final class RunnableClass
{
   public static void main(Args _args)
   {
       int myIntArray[5]; 
 
       myIntArray[1] = 11;
       myIntArray[2] = 12;
       myIntArray[3] = 13;
       myIntArray[4] = 14;
       myIntArray[5] = 15;
 
       for (int i = 1; i <= dimOf(myIntArray); i++)
       {
           int value = myIntArray[i];
           info(strFmt("%1 Element =%2", i, value));
           // Do something with the value
       }
   }
 
}

Explanation

This code is written in X++ and demonstrates the use of a regular (non-dynamic) integer array to store values and then iterates over the array to display each element's index and value using the info function.

Here's a breakdown of the code:

  1. public static void main(Args _args): This is the entry point of the program. It's a static method that takes an Args object as a parameter.

  2. int myIntArray[5];: This declares an integer array named myIntArray with a fixed size of 5. In X++, array indices start from 1, so the array can store values at indices 1 to 5.

  3. Assigning Values to Array Elements: The next few lines of code assign values to specific indices of the array:

    
    myIntArray[1] = 11;
    myIntArray[2] = 12;
    myIntArray[3] = 13;
    myIntArray[4] = 14;
    myIntArray[5] = 15;
    

    Here, each line assigns a value to a specific index of the array.

  4. for Loop: This loop iterates over the array using a counter variable i starting from 1 and ending at the size of the array (5 in this case):

    
    for (int i = 1; i <= dimOf(myIntArray); i++)
    {
        // ...
    }
    

    The loop iterates through indices 1 to 5.

  5. int value = myIntArray[i];: Inside the loop, the code retrieves the value at the current index using the counter variable i:

    
    int value = myIntArray[i];
    
  6. info(strFmt("%1 Element = %2", i, value));: This line uses the info function to display a message showing the current index and the corresponding value of the array element:

    dimOf: Retrieves the number of index elements that space has been allocated for in an X++ array.

    
    info(strFmt("%1 Element = %2", i, value));
    
    

    The strFmt function is used to format the output message.

  7. Comment: The comment // Do something with the value is a placeholder indicating that you can perform additional operations with the retrieved value if needed.

In summary, this code declares an array of integers, assigns values to its elements, and then iterates over the array to display the index and value of each element using the info function. The key takeaway is the use of regular array indices starting from 1 in X++.