Search This Blog

Thursday, 11 October 2012

Map and MapEnumerator Class

Map Class:


                       The Map class lets you associate one value, that is, the key, with another value. Both the key and value can be any valid X++ type. This includes 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.

we can also assign our hard coded key value and value in maps.

I will show you the little example on how to implement Maps.This will make more clear concepts.

Map zahidmap;
 
zahidmap=new Map(Types::Integer,Types::string);
 
zahidmap.insert(1,"Muhammad Zahid");
 
zahidmap.insert(2,"Asim Saeed");
 
zahidmap.insert(3,"Baber Tareen");
 
if(zahidmap.exist(1))
{
info(strfmt("%1",zahidmap.lookup(1)));
}
 
if(zahidmap.exist(2))
{
info(strfmt("%1",zahidmap.lookup(2)));
}
 
if(zahidmap.exist(3))
{
info(strfmt("%1",zahidmap.lookup(3)));
}

In the above code I have just created the map with key value type=integer and value type =string
than I have inserted the key value and value into the map and than i have just made an if  check whcich checks that if this key is exist or not and by m.lookup method i just return the value against the key that I have passed into the parameter.

Output:

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

 

MapEnumerator:

 
The MapEnumerator class allows you to traverse through the elements within a map.
 
Example:
 
This example is proceeding from the above created example.
 
As I have already created the map.Now in this example i will assign this map to the map enumerator.

MapEnumerator zahidmapenumerator;

zahidmapenumerator=zahidmap.getEnumerator();

while(zahidmapenumerator.movenext())
{
info(strfmt("%1",zahidmapenumerator.currentvalue()));
}

The outpur will be the same as you can see in the above image.

Thanks

Muhammad Zahid.
 
   

No comments:

Post a Comment