Multidimensional array in Microsoft Dynamics AX

So, we had the situation where we needed to use a multidimensional array within AX. AX can only handles a single dimensional array, and using the .NET classes, caused some issues when with type conversions.

Eventually we ended up creating a Class.

The example below creates a 3 dimensional array.

To test, just run the main static method.

class WeightArray3
{
Map weights;
int size1, size2, size3;
}

public static WeightArray3 construct(int _size1, int _size2, int _size3)
{
weightArray3 weightArray = new weightArray3();
weightArray.parmSize1(_size1);
weightArray.parmSize2(_size2);
weightArray.parmSize3(_size3);
weightArray.init();
return weightArray;
}

public int parmSize1(int _size1 = size1)
{
size1 = _size1;

return

public int parmSize2(int _size2 = size2)
{
size2 = _size2;

return size2;
}

public int parmSize3(int _size3 = size3)
{
size3 = _size3;

return size3;
}

public void init()
{
weights = new Map(Types::Real, Types::Integer);
}

public void setValue(Weight _weight, int _index1, int _index2, int _index3)
{
int i;
i = ((_index1 – 1) * size2 * size3)
+ ((_index2 – 1) * size3)
+ (_index3 – 1);
weights.insert(i, _weight);
}

public real getValue(int _index1, int _index2, int _index3)
{
int i;
real r;
i = ((_index1 – 1) * size2 * size3)
+ ((_index2 – 1) * size3)
+ (_index3 – 1);
r = weights.lookup(i);
return r;
}

public static void main(Args _args)
{
WeightArray3 weightArray = WeightArray3::construct(10, 5, 3);
int i1, i2, i3;
int counter = 0;
real r;

for (i1 = 1; i1 <= 10; i1++)
{
for (i2 = 1; i2 <= 5; i2++)
{
for (i3 = 1; i3 <= 3; i3++)
{
counter += 1;
weightArray.setValue(counter, i1, i2, i3);
}
}
}
for (i1 = 1; i1 <= 10; i1++)
{
for (i2 = 1; i2 <= 5; i2++)
{
for (i3 = 1; i3 <= 3; i3++)
{

r = weightArray.getValue(i1, i2, i3);
info(strfmt(‘%1 : %2 : %3 = %4’, i1, i2, i3, r));
}
}
}
}

About Johan Groenewald

I'm a Microsoft Dynamics AX Developer, working at Axnosis in Pretoria, South Africa, with over 15 years experience as a software developer and working for the past 5 years on Dynamics AX 2009 and 2012.
This entry was posted in Microsoft Dynamics AX 2012, Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *