Showing posts with label Composite Data Type. Show all posts
Showing posts with label Composite Data Type. Show all posts

Saturday, May 16, 2015

Array

Introduction:

Array is a collection of same data type variables. We can access the each element in the array by using index. The index is start with 1. Sometimes, we have to deal with same data type value collections like name of the persons. For this scenario, we can use array data type.
Array data type is one of the composite data type in AX 2012. We can’t use Array data type for store the class objects. But, we can use Array collection class instead of Array data type.
AX 2012 supports only single dimension array not multi dimension array.

Array example:

static void Hari_TestJob(Args _args)
{
    str empName[10];       // Fixed-length array with 10 integers
    empName[1] = 'Hari'; //Assign value to the 1st element in the array
    print empName[1];     //Accessing the 1st element in the array
    pause;
}

Types of array

Three types of arrays are in AX.
  1. Fixed length
  2. Dynamic length
  3. Partly on disk


Fixed length:

Declare the length of the array in the variable declaration section. For example:
    str empName[10];
As per the above declaration code, we can hold only 10 values in this array.

Dynamic length:

Declare the array without mention the length. For example:
    str empName[];
Array length is the unlimited here. Array length is calculated based on the maximum element index used.

Partly on disk:

Partly on disk is some elements occupied in memory and others are occupied on disk. We can use fixed length array and dynamic length array in the partly on disk type. We can use this type for performance optimization when deal with lot of data. For example:
    //Fixed length array in the partly on disk array type,
    //here 20 items in the memory and 100 items in the disk.
    str empName[100, 20];
    //Dynamic length array in the partly on disk array type,
    //here 20 items in the memory and other items in the disk.
    str empName[, 20];

Reset the array values:

We can reset all array element value by assign any value to the 0 element of the array. For example:
static void Hari_TestJob(Args _args)
{
    str empName[10];
    empName[1] = 'Hari';
    empName[0] = '';         // Reset the array values
    print empName[1];
    pause;
}

Get the array length:

We can get the length of the array by using the dimOf function.


Tuesday, April 7, 2015

Container Data Type in AX 2012

Introduction

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)));
}

Result:
Test
27
Second

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)));
}

Result:
First

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.