Monday, February 6, 2023

Containers

 
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.

                Container Functions:

                Below are some of the most common container functions with examples.

                conPeek() Function
                The conPeek() is used to retrieve a specific element from a container.
                Syntax: anytype conPeek(container container, int number)container - The container to return an element from.
                number - The position of the element to return. Specify 1 to get the first element.
                Return value: The element in the container at the position specified by the number parameter. The conPeek function automatically converts the peeked item into the expected return type.

                void daxErp_conPeek()
                {
                    // container declaration and initialization with 2 values
                    container daxConBeta = ["Welcome", 8594]; 
                    str charHolder;
                    ;

                    // conPeek() function returns the value being held in a specific position in the container. 
                    // (Note: It returns anyType)
                    // Here printing 1st and 2nd values from daxConBeta to info
                    charHolder = conPeek(daxConBeta, 1);
                    info(strFmt("Container daxConBeta conPeek() values are :- 
                    %1, %2", charHolder, conPeek(daxConBeta, 2)));
                }


                Reference and credits

                http://imdaxershiv.blogspot.com/2016/03/dynamics-ax-tutorial-x-containers.html

                http://daxingwitheshant.blogspot.com/2018/07/collection-classes.html

                No comments:

                Post a Comment

                Build Explained

                Useful Blogs. https://axtechsolutions.blogspot.com/2018/08/performing-builds-in-d365.html https://community.dynamics.com/blogs/post/?postid=...