Friday, 4 November 2011

Map object in AX 2009

Map:

A Map object associates one value (the key) with another value. Both the key
and value can be of any valid X++ type, including objects. The types of the key
and value are specified in the declaration of the map. The way in which maps are
implemented means that access to the values is very fast.
Multiple keys can map to the same value, but one key can map to only one value
at a time. If a [key, value] pair is added where the key already exists, it will
replace the existing pair with that key value.
The following methods are commonly used on Map objects:

• insert(anytype _key, anytype _value) - Inserts an element (pair)
into the map.

• remove(anytype _key) - Removes a (key, value) pair from a map.

• exists(anytype _key) - Determines whether a particular value exists
as a key in the map.

• lookup(anytype _key) - Returns the value mapped to by a particular
key value.

• elements() - Returns the number of elements contained in the map.

• getEnumerator() - Returns a MapEnumerator object for this Map
object.

The MapEnumerator class allows you to traverse through the elements within a
map. The following methods are commonly used on MapEnumerator objects:

• currentKey() - Retrieves the key of the pair currently pointed to in
the map.

• currentValue() - Retrieves the value of the pair currently pointed to
in the map.

• moveNext() - Moves the enumerator to the next pair in the map.
Map enumerators start before the first pair in the map, so
moveNext() must be called to make it point to the first pair in the
map.

• reset() - Moves the enumerator to the start of the map.

Map Demonstration

Demonstration - Creating a Map
The following code and procedure demonstrates how to create a map of
Customers per State, then move through it and extract values from it:

1. Create a new Map object, specifying the data type it will contain.
2. Loop through the records in the CustTable. For each, use
Map.exists() to check if CustTable.State already exists as a key in
the map.
3. If it does not already exist, use Map.insert() to insert a pair of
(CustTable.State,1).
4. If it does already exist, use Map.insert() to re-insert the pair with the
same key (which will overwrite the existing pair), but the new value
equals the existing pair value plus 1.
5. Set the MapEnumerator object using the Map.getEnumerator()
method.
6. Go to the start of the map, using MapEnumerator.reset().
7. Move through the items in the list, using
MapEnumerator.moveNext().
8. Pull the value of the current item in the list, using
MapEnumerator.currentKey() and
MapEnumerator.currentValue().
Map mapStateNumbers;
MapEnumerator enumerator;
CustTable custTable;
;
mapStateNumbers = new Map(Types::String, Types::Integer);
while select custTable
{
if(mapStateNumbers.exists(custTable.State))
mapStateNumbers.insert(custTable.State,
mapStateNumbers.lookup(custTable.State) + 1);
else
mapStateNumbers.insert(custTable.State, 1);
}
enumerator = mapStateNumbers.getEnumerator();
while (enumerator.moveNext())
{
info(strfmt("%1 customers are located in %2",
enumerator.currentValue(), enumerator.currentKey());
}

No comments:

Post a Comment