Monday, November 10, 2014

Containers and functions in AX 2012 & 2009


A container variable can contain different types and values of simple and extended data types, including arrays and other container variables. Classes cannot be put into containers.
There are many functions that manipulate container variables:
conPeek()
conDel()
conNull()
conFind()
conIns()
conPoke()
conLen()

condel :- Use condel to delete one or more items from a container.

EX:-  container conDel(container container, int start, int number)
 
static void conDelExample(Args _args)
{
    container c = ["Hello world", 1, 3.14];
    ;
    // Deletes the first two items from the container.
    c = conDel(c, 1, 2);
}
 
confind:- Use confind to locate a sequence of items in a container.

EX:-  int conFind (container container, anytype element,... )

static void conFindExample(Args _args)
{
    container c = ["item1", "item2", "item3"];
    int i;
    int j;
    ;
 
    i = conFind(c, "item2");
    j = conFind(c, "item4");
    print "Position of 'item2' in container is " + int2Str(i);
    print "Position of 'item4' in container is " + int2Str(j);
    pause;
}

conins :-  Use conins to insert some items into a container.
EX:- container conIns (container container ,int start ,anytype element,... )
 
static void conInsExample(Args _arg)
{
    container c;
    int i;
    ;
    c = conIns(c,1,"item1");  
    c = conIns(c,2,"item2");
    for (i = 1 ; i <= conLen(c) ; i++)  
    {
    // Prints the content of a container.
        print conPeek(c, i);
    }
    pause;
}
 

conlen :- Use conlen to find out how many items there are in a container.
EX:- int conLen(container container)
 
static void conLenExample(Args _arg)
{
    container c;
    int i;
    ;
 
    c = conins(["item1", "item2"], 1);  
    for (i = 1 ; i <= conLen(c) ; i++)
    {
        print conPeek(c, i);
     }
    pause;
}

connull :- Use connull to explicitly dispose of the contents of a container.
EX:- container conNull()
 
static void conNullExample(Args _arg)
{
    container c = ["item1", "item2", "item3"];
    ;
 
    print "Size of container is " + int2str(conLen(c));
    // Set the container to null.
    c = conNull();  
    print "Size of container after conNull() is " + int2Str(conLen(c));
    pause;
}

conpeek:- Use conpeek to extract an item from a container, and to convert it into another data type, if necessary.
EX:- anytype conPeek(container container, int number)
 
static void main(Args _args)
{
    container cnI, cnJ;
    int i, j;
    anytype aty;
    
    
    info("container cnI ...");
    cnI = ["itemBlue", "itemYellow"];
    
    for (i=1; i <= conLen(cnI); i++)  
    {
        aty = conPeek(cnI, i);
        info(int2str(i) + " :  " + aty);
    }
 
    //
    
    info("container cnJ ...");
    cnJ = conIns(cnI, 2, "ItemInserted");
    
    for (j=1; j <= conLen(cnJ); j++)  
    {
        aty = conPeek(cnJ, j);
        info(int2str(j) + " :  " + aty);
    }
}

conpoke :- Use conpoke to replace (poke) an item in a container.

EX:- container conPoke(container container,int start,anytype element,...)
 
static void conPokeExample(Args _arg)
{
    container c1 = ["item1", "item2", "item3"];
    container c2;
    int i;
 
    void conPrint(container c)
    {
        for (i = 1 ; i <= conLen(c) ; i++)
        {
            print conPeek(c, i);
        }
    }
    ;
    conPrint(c1);
    c2 = conPoke(c1, 2, "PokedItem");
    print "";
    conPrint(c2);
    pause;
}

No comments:

Post a Comment