Using dimof() on a Dynamic Array

I stumbled across some older X++ code that was using the little know dynamic array. It took a while to find out how to retreive the size of the dynamic array.  Here is a little example of using a dynamic array in Axapta:

 

int foo[];

int r;

;

foo[10] = 1;

r = dimof(foo); // r will be 16 here

foo[100] = 3;

r = dimof(foo); // r will be 112 here

 

As you can see there is no special syntax required to access individual elements, simply start stuffing values in. Like all Axapta arrays it is limited to a single dimension and basic types (that is no objects). The array grows in 16 element chunks. You can find the current dimensions of the array using the dimof(). Note that this is the actually allocated length of the array and not the highest element actually used so far. If you need this information you will have to store it separately. A higher level abstraction that removes many of these limitations can be found in the Array class.