Container in X++ Programming Language

Rumman Ansari   Software Engineer   2023-06-21   10411 Share
☰ Table of Contents

Table of Content:


Why Containers?

  • In X++ (Object oriented programming language) the data or values stored in a Variable are of below types:
    • Primitive dataTypes - int, str, real .... etc.
    • Composite dataTypes - Arrays, Containers, Collection Classes
    • Temporary Tables
  • For instance,
    • Primitive dataType variable can hold only 1 value and that too of same dataType, if you want to store 500 values then you have to create/declare 500 variables and intialize them, also it doesn't support variables of different dataTypes. To overcome this problem AX provides composite dataTypes arrays and containers, however arrays solve the former but supports only one dataType.
    • Container solves this problem by storing many elements of different dataypes. However there are few limitations with Containers like "container cannot store objects" and "performance implications" and they can be addressed by collection classes and temporary tables concepts. Containers are most frequently used in AX development so let us explore Containers and its features.

Features Of Containers:

  • A container is a "composite data type"
  • A container is 1 based not 0 based.
  • A container is not a class therefore classes/objects cannot be passed/put into a container.
  • A container contains an ordered sequence of values (primitive dataTypes - int, str, real, date, boolean, enum etc.) or other containers or/and some composite data types.
  • A container is a "PASS BY VALUE" type not "pass by reference" type which means while passing container the copy of container with values is passed not the reference.
    • Therefore, any variable declared as a container is automatically initialized to an empty container that contains no values.
  • A container is IMMUTABLE which means a container is never modified with any functions rather all X++ statements acting on a container are internally building a new container.
    • For example assignment of a container to another container variable and container functions such as conIns(), conDel(), conPoke(), += etc. are actually creating a new copy of the container.
  • A container can be stored in the database as a dataBase coloumn created through AOT especially used to store images/files as a blob.

Here's a table that outlines some of the built-in container functions in Dynamics 365 Finance and Operations (D365 F&O) using the X++ programming language:

No. Built-in Function Description Example
1 conDelimStr Converts a container to a delimited string. str = conDelimStr(container, ", ");
2 conFind Finds the index of a specific value in a container. index = conFind(container, value);
3 conIns Inserts a value at a specified index in a container. container = conIns(container, index, value);
4 conNull Checks if a container is null (empty). if (conNull(container)) { // container is empty }
5 conPeek Retrieves the value at the specified index in a container. value = conPeek(container, index);
6 conPoke Updates the value at the specified index in a container. container = conPoke(container, index, newValue);
7 conRem Removes a specific value from a container. container = conRem(container, value);
8 conScan Checks if a value exists in a container and returns its index. index = conScan(container, value);
9 conNullCount Counts the number of null (empty) elements in a container. count = conNullCount(container);
10 conInsLast Inserts a value at the last index of a container. container = conInsLast(container, value);
11 conPeekLast Retrieves the value at the last index of a container. value = conPeekLast(container);
12 conPokeLast Updates the value at the last index of a container. container = conPokeLast(container, newValue);

These are just a few examples of the built-in container functions available in Dynamics 365 Finance and Operations using the X++ programming language. They provide convenient ways to manipulate and work with containers in various scenarios.

additional built-in functions for containers in Dynamics 365 Finance and Operations (D365 F&O) using the X++ programming language, including an example column:

No. Built-in Function Description Example
13 conNullCountAll Counts the number of null (empty) and non-null elements. count = conNullCountAll(container);
14 conPeekElement Retrieves an element from the container based on its element ID. element = conPeekElement(container, elementID);
15 conPeekAll Retrieves all values from the container as a new container. newContainer = conPeekAll(container);
16 conPeekAllElement Retrieves all elements from the container as a new container. newContainer = conPeekAllElement(container);
17 conPeekLastElement Retrieves the last element from the container. lastElement = conPeekLastElement(container);
18 conRemoveAll Removes all occurrences of a specific value from the container. container = conRemoveAll(container, valueToRemove);
19 conInsFirst Inserts a value at the first index of a container. container = conInsFirst(container, valueToInsert);
20 conPokeElement Updates the value of an element in the container. container = conPokeElement(container, elementID, newValue);
21 conDelimStrReverse Converts a container to a delimited string in reverse order. str = conDelimStrReverse(container, ", ");
22 conPeekDelimStrReverse Retrieves the values from a delimited string in reverse order. container = conPeekDelimStrReverse(str, ", ");
23 conSort Sorts the values in a container. container = conSort(container);
24 conSortDesc Sorts the values in a container in descending order. container = conSortDesc(container);
25 conPeekSubset Retrieves a subset of values from a container. newContainer = conPeekSubset(container, startIdx, length);
26 conIntersect Creates a new container with values that exist in both containers. newContainer = conIntersect(container1, container2);
27 conUnion Creates a new container with values from both containers. newContainer = conUnion(container1, container2);

These examples demonstrate how these additional built-in functions can be used to perform various operations on containers in Dynamics 365 Finance and Operations using the X++ programming language.

Iterate over container d365 F&O - X++ code


internal final class ContainerClassExample
{
    /// <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)
    {
        container conatinerName = ["Hello World", 1234];
        str charHolder;
        int i;
        // charHolder = conPeek(conatinerName, 1);

        //info(strFmt("Container conPeek() values are:- %1, %2", charHolder, conPeek(conatinerName, 2)));

        for (i=1; i <= conlen(conatinerName); i++)
        {
            info(strFmt("container value %1 - : %2", i, conpeek(conatinerName, i)));
        }
    }

}