Understanding Lists in X++ Language: Concepts and Practical Applications

Rumman Ansari   Software Engineer   2024-11-28 08:44:47   1537  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

Introduction:

X++ is a versatile programming language used in Microsoft Dynamics 365 Finance and Operations (D365 F&O) for developing robust business applications. One of the powerful data structures in X++ is the list. In this blog post, we will explore the concept of lists, their features, and how they can be leveraged for efficient data manipulation.


1. What is a List?

A list in X++ is a dynamic data structure that allows you to store and manage a collection of elements of the same type. Unlike fixed-size arrays, lists have a dynamic size that can grow or shrink based on the number of elements. This flexibility makes lists an excellent choice for handling varying amounts of data.


2. Declaring and Initializing a List:

To create a list, you need to declare it with the desired element type and initialize it. Here's an example:


List myList = new List();

3. Adding Elements:

You can add elements to a list using the `addEnd()` method, which appends an element at the end of the list. Alternatively, the `add()` method allows you to insert an element at a specific index.


myList.addEnd(5); // Appends 5 at the end of the list
 

4. Accessing Elements:

Elements in a list can be accessed using their index. X++ lists use 1-based indexing, where the first element is at index 1.


int element = myList[3]; // Retrieves the element at index 3


5. Removing Elements:


 

6. Iterating through a List:


 internal final class ListExampleDemo
{
   /// <summary>
   /// Entry point of the program. 
   /// Demonstrates how to use a List and ListIterator in X++.
   /// </summary>
   /// <param name="_args">Runtime arguments passed to the program.</param>
   public static void main(Args _args)
   {
       // Step 1: Initialize a list to store integers
       List integerList = new List(Types::Integer); 
       ListIterator listIterator; 
       int currentNumber;

       // Step 2: Add numbers from 1 to 10 to the list
       for (currentNumber = 1; currentNumber <= 10; currentNumber++) 
       { 
           integerList.addEnd(currentNumber); 
       }

       // Step 3: Initialize an iterator to traverse the list
       listIterator = new ListIterator(integerList); 

       // Step 4: Traverse and print each number in the list
       while (listIterator.more()) 
       { 
           int numberToPrint = listIterator.value(); // Get the current value
           print strFmt("Number: %1", numberToPrint); // Print the value
           listIterator.next(); // Move to the next element
       }
 
   }
}
  • When a ListIterator object is created in X++, it is automatically positioned at the first element of the list.
  • If the list is not empty, the iterator points to the first value.
  • In Dynamics 365 Finance and Operations (D365 F&O), the platform differentiates between client and server execution contexts.
  • A List object resides either on the client side or the server side, based on where it was created.
  • An iterator (ListIterator) must execute in the same context (client or server) as the List.
  • If the iterator and list are not in the same context, a runtime error may occur.

Why?

  • This is because lists are not automatically synchronized between client and server. Attempting to access an object in the wrong context violates the separation of environments in D365 F&O.

Conclusion:

Lists are valuable data structures in X++ that provide flexibility and efficiency in handling collections of elements. With their dynamic size and various methods for manipulation, lists empower developers to effectively manage data in X++ applications. By leveraging lists, you can build more scalable and maintainable solutions in Microsoft Dynamics 365 Finance and Operations.

Note: The examples and code snippets provided in this blog post are intended for illustrative purposes and may need to be adapted based on specific requirements and coding practices.


Example code


internal final class ListClass
{
    /// <summary>
    /// Class entry point. The system will call this method when a designated menu 
    /// is selected or when execution starts and this class is set as the startup class.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args){
        List myList = new List(Types::Integer);
        List myListString = new List(Types::String);
        ListIterator literator;

            myList.addStart(12);

        // add the element at the end of the list
        //
        myList.addEnd(2);

        // add the element at the start of the list
        myList.addStart(3);

        myList.addEnd(7);

        myListString.addEnd("Second");
        myListString.addStart("First");
        myListString.addEnd("Third");
        myListString.addEnd("Fourth");

        // If you want to insert the data at some specific index, then you need to make use of the listIterator class
        // Iterator performs a midpoint

        // insert at current position.
        literator = new ListIterator(myListString);

        while (literator.more())
        {
            // can provide some condition, i.e. if check etc
            if (literator.value())
            {
                Info(strFmt("%1 ", literator.value() ));
                literator.next();
              
            }
            else
            {
                break;
            }
        }

    }

}