Friday, June 16, 2017

ARRAYS :MICROSOFT DYNAMICS 365:DYNAMICS AX 2012 X++

ARRAYS

An array is a collection of variables that are all of the same type. The elements of an array are accessed with simple integer indexes. For example:
int myArray[10]; // Fixed-length array with 10 integers
myArray[4] = 1; // Accessing the 4th element in the array
You use a separate statement to initialize each element in an array.
Note
When you use a Container data type or an Array object to create a collection, you can initialize multiple elements by using a single statement.
There are three kinds of arrays:
·         Dynamic
·         Fixed-length
·         Partly on disk
X++ only supports one-dimensional arrays. It is possible, however, to mimic the behavior of multiple array indices.
Note
Variables in objects and tables can be declared as arrays (this is used in address lines in the standard application).
An Array collection class enables you to store objects in an array.

Declaring Arrays

Array declaration
=
datatype Variable { , Variable } ;
Variable
=
Identifier arrayoptions
Arrayoptions
=
[ [ Length ] [, Memory ] ]
If a Length is specified, the array is a fixed-length array with Length elements. Otherwise, it is a dynamic array.
If Memory is specified, it is a partly on disk array.
X++
static void Job1(Args _args)
{
    // A dynamic array of integers
    int i[]; 
 
    // A fixed-length real array with 100 elements
    real r[100]; 
 
    // A dynamic array of dates with only 10 elements in memory
    date d[,10]; 
 
    // A fixed length array of NoYes variables with
    // 100 elements and 10 in memory
    NoYes ny[100,10];
    
    print "Done.";
    pause;
}

Array Indices

Array indexes begin at 1. The first item in the array is called [1], the second [2], and so on.
The syntax for accessing an array element is:
ArrayItemReference = ArrayVariable [ Index ]
where ArrayVariable is the identifier of the array, and Index is the number of the array element. Index can be an integer expression.
For example, a[9] accesses item number 9 in array a.
In X++, item zero[0] is used to clear the array! Assigning a value to index 0 in an array resets all elements in the array to the default value. For example,
intArray[0] = 0; //Resets all elements in intArray

Dynamic Arrays

A dynamic array is declared with an empty array option (that is, only square brackets):
//Dynamic array of integers
int intArray[];
// Dynamic array of variables of type Datatype
Datatype arrayVariable[];

Fixed-length Arrays

A fixed-length array can hold the number of items that is specified in the declaration. Fixed-length arrays are declared like dynamic arrays but with a length option in the square brackets:
boolean boolArray[100]; //Fixed-length array of booleans with 100 items

Partly On Disk Arrays

Partly on disk arrays are declared either as dynamic or fixed-length arrays with an extra option that declares how many items should be held in memory. The other items are stored on disk and automatically loaded when referenced.
//Dynamic integer array with only 100 elements in memory.
 
int arrayVariable [ ,100];
//Fixed-length string array with 1000 elements, and only 200 in memory.
 
str arrayVariable [1000,200]

Multiple Array Indexes

Some languages, such as C++ and C#, allow you to declare arrays with more than one index; that is, to define "arrays of arrays." You cannot directly create multiple array indexes in X++ - only one-dimensional arrays are supported. However, you can implement multiple indexes by using the following scheme.
If you wanted to declare an array with two dimensions, for example, for holding an amount earned by country by dimension, and there were 10 countries and 3 dimensions, declare the following:
real earning[10, 3];
This is not possible in Microsoft Dynamics AX. Instead, you can define a one-dimensional array with the number of elements that is the product of the elements in each dimension:
real earnings[10*3];
To refer to earnings[i,j], write:
earnings[(i-1)*3 +j]
You could wrap this into a macro:
#localmacro.earningIndex
(%1-1)*3+%2
#endmacro
And then write:
earnings[#earningIndex(i,j)]
The previous scheme can be extended to any number of dimensions. The element a[i1, i2, ..., ik] can be accessed by computing the offset into an array containing (d1*d2*...*dk) elements:
(i1 - 1)*d2*d3*..*dk +
(i2 - 1)*d3*d4*...*dk + .... +
(ik-1 -1)*dk +

(ik-1)

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...