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

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

Table of Content:


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++.