Saturday 29 March 2014

List, Set and Map in Dynamics Ax 2012 (Collection classes)

List :

      - It will hold similar datatype
      - Imp methods - elements. addstart, addend

Limitation of list - it will give in the order u/developer add - ascending or descending order  it all also allow duplicates


static void TDS_ListExample(Args _args)
{
    List l = new List(Types::String);
    ListIterator li;
    ;
   
    // limitation of list - it will give in the order u add - ascending or descending order
    // it all also allow duplicates
   
   
    l.addStart("Tendulkar");
    l.addEnd("sehwag");
    l.addEnd("gambhir");
    l.addEnd("yuvraj");
    l.addStart("haribhajan");
    l.addend("haribhajan");
   
    info(int2str(l.elements()));
   
    li = new ListIterator(l);
   
    while (li.more()) // this will return true or false -
    {
        info (li.value());
        li.next();
    }
   

}

Set : 

      - To overcome the limitations of list, we can use sets

static void TDS_SetExample(Args _args)
{
    Set s1 = new Set(Types::String);
    SetIterator si;
   
    Set s2 = new Set(Types::String);
   
    Set resultSet;
   
    // set will always result in ascending order and it will not allow duplicates
    // set can perform additional operations - union , intersection and difference
   
   
    s1.add("A");
    S1.add("z");
    s1.add("B");
    s1.add("F");
    s1.add("F");
   
    // To find any value in a set use "in" method
    if (s1.in("b") == true)
    {
        info ("b exists");
    }
    else
    {
        error ("b doesnt exists");
    }
   
   
    s2.add("A");
    s2.add("k");
    S2.add("g");
   
    info(int2str(s1.elements()));
   
    si = new SetIterator(s1);
   
    while (si.more())
    {
        info(si.value());
        si.next();
    }
   
    resultSet = Set::intersection(s1, s2);
   
    si = null;
   
    info ("Intersection");
    si = new SetIterator(resultSet);
   
     while (si.more())
    {
        info(si.value());
        si.next();
    }

   
}


Map : 

        - Map can hold a key and associated value
        - Imp methods : elements, Key, insert

static void TDS_MapExample(Args _args)
{
    // Maps are used to hold the key and correspondiong value
   
    Map m = new Map(Types::Integer, Types::String);
    MapIterator mi;
    ;
   
    m.insert(100, "Nick");
    m.insert(200, "Leo");
    m.insert(400, "Eve");
   
    mi = new MapIterator(m);
   
    while (mi.more())
    {
        info(mi.key());
        info(mi.value());
        mi.next();
    }
}

Another example to load all the records from the table to the map

Here key is accountNumber which is tring and associated value is creditlimit which is real

static void MAP_LoadTableTable(Args _args)
{
   Map m = new Map(Types::String, Types::Real);
   MEE_CustTable k;
   MapIterator mi;
 
 
   ;
 
   while select k
   {
        m.insert(k.CustomerId, k.Creditlimit);
   }
 
   info(int2str(m.elements()));
 
   info ("_______________________");
 
    mi = new MapIterator(m);
   
    while (mi.more())
    {
        info(mi.key());
        info(mi.value());
        mi.next();
    }

}


For all the classes , use iterator classes to print or get the values
ListIterator, MapIterator, SetIterator.

Note : Containers, List, Set and Map all together are called as collection classes in Dynamics Ax 2012. 

Hope u find this data useful... :)




No comments:

Post a Comment