Monday, May 18, 2015

Ternary Operator (?)

Introduction:

Ternary operator is a conditional statement. It is related to the If condition, but it assigns value to the variable. Based on the scenario, we can use ternary operator instead of If condition.

Syntax:

expression1 ? expression2 : expression3

Expression1 must be a Boolean expression. Expression1 may be a condition for example:

salaryDeduction = calculatedDeduction > 500 ? calculatedDeduction - 500 : 0;

expression2 and expression3 must be a same data type otherwise, it will be consider as error. You may get warning/error message like “Operand types are not compatible with the operator”.

As per the above code if the calculatedDeduction is more than 500 then expression2 result value will be assigned to the variable salaryDeduction else 0 will be assigned to the variable salaryDeduction. It is also possible to use ternary statement nested inside another ternary statement. Last time, I tried this ternary operator in the update_recordset, but not supported.

Replace If condition:

Based on the scenario, we can use ternary operator instead of if condition.

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.