We cannot store objects in arrays (x++ class) or containers. The Microsoft Dynamics AX collection classes have been designed for storing objects.
Collection classes were formerly called Foundation classes.
Below are collection classes
* Set
* Map
* List
* Array
* Struct
Set:
* Set Holds the value of any single type.
* Values are not stored in the sequence they are added. Instead, the Set object stores them in a * manner that optimizes performance for the in method.
* It will not Allow the Duplicate Values.
* To overcome the limitations of list We use Set.
* It has Add method.
Example:
static void set(Args _args)
{
// Create a new set of type integer
Set s= new Set (Types::Integer);
SetEnumerator setenum;
// Add elements to the set
s.add( 20);
s.add( 25);
s.add( 20);
// Get the enumerator of the set
// to loop through it
setenum= s.getEnumerator();
While (setenum.movenext())
{
Info (strfmt('Value is: %1 ',setenum.current()));
}
}
Map:
* Map Associates a key value with another value.
* It doesn't allow duplicate Key values in Maps.
* Maps sorted based on Key Value.
* It has an Insert Method.
Example:
static void Map(Args _args)
{
Map demomap;
MapEnumerator enumerator;
// Creating a map of Valuetype is string and key value is integer
demomap = new Map(Types::String, Types::Integer);
// Adding Elements To the Map
demomap.insert("jaipal", 1);
demomap.insert("jadav", 2);
//Get the Enumerator For Map
// for looping the all elements
enumerator = mapTest.getEnumerator();
while (enumerator.moveNext())
{
info(strfmt("Key - %1 , Value %2.",enumerator.currentKey(),enumerator.currentValue()));
}
}
We can also use Iterators for looping but here we are using Enumerators.
Iterators replaced because of a few unwarranted drawbacks that appear as hard-to-find errors.
List:
* It will hold similar datatype.
* It retrieves the the way we are given
* It allows the duplicate values.
* List class provides an addStart method, and AddEnd method.
Example:
static void Collection_List()
{
// Create a new list of type string
List names = new List(Types::String);
ListEnumerator listE;
// Add elements to the list
names.addEnd("jaipal");
names.addEnd("jadav");
names.addStart("Srikanth");
// Get the enumerator of the list
// to loop through it
listE = names.getEnumerator();
while (listE.moveNext())
{
info (strfmt("Name: %1", listE.current()));
}
}
Struct:
A struct holds several values of any X++ type, to group the information about a specific entity.
Example:
static void Collection_Struct()
{
// Create a struct with two fields
struct myCar = new struct (“int ModelYear; str Carbrand”);
int i;
;
// Set values to the fields
myCar.value(“ModelYear”, 2015);
myCar.value(“Carbrand”, “BMW”);
// Add a new field and give it a value
myCar.add(“Model”, “320”);
// Loop through the fields of the struct
for (i=1; i<=myCar.fields(); i++)
{
info(strfmt(“FieldType: %1, FieldName: %2, Value: %3”,
myCar.fieldType(i),
myCar.fieldName(i),
myCar.value(myCar.fieldName(i))));
}
}
No comments:
Post a Comment