MAPS IN AX 2012 MAP ENUMERATOR
The (key, value) pairs in a map can be traversed using the MapEnumerator class.
Examples
The following example illustrates how to invert a map. It is only possible to invert a map if no value is mapped to by two different keys. Comparing the number of elements in the Map.keySet method and the Map.valueSet method checks this. Otherwise, the elements are traversed in the incoming map and inserted in the result map. The function that does the inversion, that is, the invertMap method, will work regardless of the types of the keys and values.
X++
{
Map example;
Map invertMap(map _mapToInvert)
{
MapEnumerator en;
Map result = new Map(
_mapToInvert.valueType(),
_mapToInvert.keyType());
if (_mapToInvert.keySet().elements()
!= _mapToInvert.valueSet().elements())
{
return null;
}
en = new MapEnumerator(_mapToInvert);
while (en.moveNext())
{
result.insert(en.currentValue(), en.currentKey());
}
return result;
}
;
// Fill in a few values.
example = new Map(Types::Integer, Types::String);
example.insert (1, "one");
example.insert (2, "two");
print invertMap(example).toString();
pause;
// Now two keys (2 and 3) map to the same value
// so can't create inverse map
example.insert (3, "two");
if (!invertMap(example))
{
print "Could not create the map";
}
pause;
}
Reference :https://msdn.microsoft.com
No comments:
Post a Comment