Tuesday, April 7, 2015

Container Data Type in AX 2012


Container is a one of the data type in ax. We can hold more primitive data type values in a single container. Container index starts by 1 not 0. For example:

container   c = ['Test', 27];
info(strFmt('%1 - %2', conPeek(c, 1), conPeek(c, 2)));

Result: Test – 27

Table Field Type:

Container is one of the table field data type. So, we can store the container directly in to the database table field. In SQL, container table field data type is treated as varbinary datatype field.

Store container in another container:

We can also store one container in another container. For example:

int         i;
container   c = ['Test', 27], d = ['Second'];   
c += d;
for(i = 1; i <= conLen(c); i++)
    info(strFmt('%1', conPeek(c, i)));


Value based:

It is a value based structure. We can’t store any class objects in this container. It is not a reference based data type. If you assign one container in another container, the container will be copied not linked by reference. For example:

int         i;
container   c = ['First'], d;   
c += d;
d += ['Second'];
for(i = 1; i <= conLen(c); i++)
    info(strFmt('%1', conPeek(c, i)));


When we use container

As per the above code, we add d container in c and adding “Second” string value in the d container after adding. But “Second” value is not added in the c container. So, if you pass container as argument to the method, it creates new container. So, performance will be slow.
If you want to handle different data types, container is the good choice. But, if you add repeated value like list in the container, it is not a good choice, because it will affect performance.

Containers are immutable

Immutable means once the variable is declaration, we can’t change the size. Here container is an immutable object. When we add, delete and update the container, new container will create.

The following statements all create a new container.

myContainer = [1]; // new container created for myContainer
myContainer += [2]; // new new container created for myContainer
myContainer4 = myContainer5; // new container created for myContainer4

Insert performance:

+= is faster than conIns function. We can use conIns when you want to insert the element in the particular index.

No comments :

Post a Comment