Sunday, 10 October 2010

AX - How to use Map and MapEnumerator

Similar to Set class, Map class allows you to associate one value (the key) with another value. Both the key and value can be any valid X++ type, including objects. The types of the key and the 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.
Below is a sample code that sets and retrieves values from a map.
static void checkItemNameAliasDuplicate(Args _args)
    inventTable         inventTable;
    Map                 map;
    MapEnumerator       mapEnumerator;
    NameAlias           nameAlias;
    int                 counter = 0;

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

    //store into map
    while select inventTable
        nameAlias = inventTable.NameAlias;
        if (!map.exists(nameAlias))
            map.insert(nameAlias, 1);
            map.insert(nameAlias, map.lookup(nameAlias) + 1);

    //retrieve from map
    mapEnumerator = map.getEnumerator();
    while (mapEnumerator.moveNext())
        nameAlias       = mapEnumerator.currentKey();


No comments: