Container in X++ Programming Language

 

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 FunctionDescriptionExample
1conDelimStrConverts a container to a delimited string.str = conDelimStr(container, ", ");
2conFindFinds the index of a specific value in a container.index = conFind(container, value);
3conInsInserts a value at a specified index in a container.container = conIns(container, index, value);
4conNullChecks if a container is null (empty).if (conNull(container)) { // container is empty }
5conPeekRetrieves the value at the specified index in a container.value = conPeek(container, index);
6conPokeUpdates the value at the specified index in a container.container = conPoke(container, index, newValue);
7conRemRemoves a specific value from a container.container = conRem(container, value);
8conScanChecks if a value exists in a container and returns its index.index = conScan(container, value);
9conNullCountCounts the number of null (empty) elements in a container.count = conNullCount(container);
10conInsLastInserts a value at the last index of a container.container = conInsLast(container, value);
11conPeekLastRetrieves the value at the last index of a container.value = conPeekLast(container);
12conPokeLastUpdates 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 FunctionDescriptionExample
13conNullCountAllCounts the number of null (empty) and non-null elements.count = conNullCountAll(container);
14conPeekElementRetrieves an element from the container based on its element ID.element = conPeekElement(container, elementID);
15conPeekAllRetrieves all values from the container as a new container.newContainer = conPeekAll(container);
16conPeekAllElementRetrieves all elements from the container as a new container.newContainer = conPeekAllElement(container);
17conPeekLastElementRetrieves the last element from the container.lastElement = conPeekLastElement(container);
18conRemoveAllRemoves all occurrences of a specific value from the container.container = conRemoveAll(container, valueToRemove);
19conInsFirstInserts a value at the first index of a container.container = conInsFirst(container, valueToInsert);
20conPokeElementUpdates the value of an element in the container.container = conPokeElement(container, elementID, newValue);
21conDelimStrReverseConverts a container to a delimited string in reverse order.str = conDelimStrReverse(container, ", ");
22conPeekDelimStrReverseRetrieves the values from a delimited string in reverse order.container = conPeekDelimStrReverse(str, ", ");
23conSortSorts the values in a container.container = conSort(container);
24conSortDescSorts the values in a container in descending order.container = conSortDesc(container);
25conPeekSubsetRetrieves a subset of values from a container.newContainer = conPeekSubset(container, startIdx, length);
26conIntersectCreates a new container with values that exist in both containers.newContainer = conIntersect(container1, container2);
27conUnionCreates 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)));
        }
    }

}

Comments