Need help in using Axapta Collections Class

Hi,

I have some problems in using Axapta Collections class, i need to combine container and list.

Example in python code:

testDict = {} // declare dictionary

testList = [] // declare list

testDict = {‘size’:[1,2,3,4,5]} // Assign value

When calling the dictionary:

testDict[‘size’][0] // it will shows out result 1

How to simulate similar way in Axapta?

thank you very much

X++ collection classes don’t have indexers, so you have to use method calls. Also don’t expect that collections with same names behave in the same way in different languages.

This is a close approximation of your code in X++:

Map map = new Map(Types::String, Types::Class);
Array arrayList = new Array(Types::Integer);

arrayList.value(1, 1);
arrayList.value(2, 2);
arrayList.value(3, 3);
arrayList.value(4, 4);
arrayList.value(5, 5);

map.insert('size', arrayList);

arrayList = map.lookup('size');
info(int2str(arrayList.value(1)));

Thank you Martin,

But i have another concern, i tried to insert another element in map.insert(‘type’, arrayList);

When i call it out,

arrayList = map.lookup(‘type’);

info(int2str(arrayList.value(1)); // The value here is getting from the value of ‘size’ : Result = 1

Can the arrayList be separated, based on size and based on type? so when we loop out the result will shows based on lookup().

Thank you so much.

“Can the arrayList be separated?” -What do you mean. Each variable can held only one value at given time - either you need more variables, or different timing (so you don’t overwrite the reference in wrong moment). Please show us your code if you want a concrete answer.

Maybe you should explain what you try to achieve - I suspect you try use the Python way without knowing the X++ way.

static void Test(Args _args)

{

Map TestMap = new Map(Types::String, types:Class);

Array TestArray = new Array(Types::String);

While Select * From MSetup // Example: This MSetup only contain 2 value, which is 1. Size, 2. Type

{

While Select * From MSpecification where MSpecification.SetupName == MSetup.SetupName // Example: This MSpecification contain value For SetupName(Size) = Size1, Size2, Size3 and For SetupName(Type) = Type1, Type2, Type3

{

TestArray.value(i, MSpecification.SpecName);

}

TestMap.insert(MSetup.SetupName, TestArray);

}

// Expected Results for

TestArray = map.lookup(‘Size’);

For (i=1, i<10, i++)

{

info(TestArray.value(i)); // Expected results should be = Size1, Size2, Size3.

}

// Expected Results for

TestArray = map.lookup(‘Type’);

For (i=1, i<10, i++)

{

info(TestArray.value(i)); // Expected results should be = Type1, Type2, Type3.

}

}

Thanks Martin for the help, i wanna know how to implement this kind of example in X++.

This can’t be your actual code - it’s full of compilation errors!

This is a working equivalent of your code:

Map     testMap   = new Map(Types::String, Types::Class);
Array   testArray = new Array(Types::String);
int     i;
;

testArray.value(1, 'Size1');
testArray.value(2, 'Size2');
testArray.value(3, 'Size3');
testMap.insert('Size', testArray);

testArray.value(1, 'Type1');
testArray.value(2, 'Type2');
testArray.value(3, 'Type3');
testMap.insert('Type', testArray);

testArray = testMap.lookup('Size');
for (i=1; i<=3; i++)
{
    info(testArray.value(i));
}

testArray = testMap.lookup('Type');
for (i=1; i<=3; i++)
{
    info(testArray.value(i));
}

It’s still doesn’t do what you want, because you have only one array instance, not two, so you overwrite values for sizes.

Now try this:

Map     testMap   = new Map(Types::String, Types::Class);
Array   testArray;
int     i;
;

testArray = new Array(Types::String);
testArray.value(1, 'Size1');
testArray.value(2, 'Size2');
testArray.value(3, 'Size3');
testMap.insert('Size', testArray);

testArray = new Array(Types::String);
testArray.value(1, 'Type1');
testArray.value(2, 'Type2');
testArray.value(3, 'Type3');
testMap.insert('Type', testArray);

testArray = testMap.lookup('Size');
for (i=1; i<=3; i++)
{
    info(testArray.value(i));
}

testArray = testMap.lookup('Type');
for (i=1; i<=3; i++)
{
    info(testArray.value(i));
}

The problem is not AX-specific at all - it’s about understanding how reference types work.

By the way, read data directly from table buffers whenever possible, it’s easier than creating complicated collections.