Container Functions in X++ Programming Language

 

ConPeek() Function

·         The conPeek() is used to retrieve a specific element from a container.

·         Syntax: anytype conPeek(container container, int number)

o    container - The container to return an element from.

o    number - The position of the element to return. Specify 1 to get the first element.

o    Return value: The element in the container at the position specified by the number parameter. The conPeek function automatically converts the peeked item into the expected return type.

Example

 
 
 
static void JobConatinerExample1(Args _args)
{
    // container declaration and initialization with 2 values
    container conatinerName = ["Hello World", 1234]; 
    str charHolder; 
    // conPeek() function returns the value being held in a specific position in the container. 
    // (Note: It returns anyType)
    // Here printing 1st and 2nd values from conatinerName to info
    charHolder = conPeek(conatinerName, 1);
    info(strFmt("Container conPeek() values are
           :- %1, %2", charHolder, conPeek(conatinerName, 2)));
}
 

Output

 

Info       Message (05:17:28 am)
             Container conPeek() values are :- Hello World, 1234



ConPok() Function

 

 

·         The conPoke() is used to modify a container by replacing one or more of the existing elements.

·         Syntax: container conPoke(container container, int start, anytype element, ...)

o    container - The container to modify.

o    start - The position of the first element to replace.

o    element - One or more elements to replace, separated by commas.

·         Return value: The new container with the inserted elements.

Example:

Code:

 
 
static void Container_conPokeExample(Args _args)
{
    // container declaration and initialization with 2 values
    container containerName = ["Welcome", 1202];  
 
    // conPoke() function usage 
    // (Replaces the value being held in a specific position in the container, with a new value)
    // Note: Here Replacing 2nd value (1202) with (29) in containerName
    containerName  = conPoke(containerName, 2, 29);
    info(strFmt("Container containerName conPoke() values are :- %1, %2", conPeek(containerName, 1), conPeek(containerName, 2)));
}
 
 

Output:

The above code will produce the following result-

 
Container containerName conPoke() values are :- Welcome, 29
 

 

conIns() Function

·         The conIns() is used to insert one or more elements into a container.

·         Syntax: container conIns(container container, int start, anytype element, ...)

o    container - The container into which to insert elements.

o    start - The position at which to insert elements.

o    element - One or more elements to insert, separated by commas.

·         Return value: The new container with the inserted elements.

Example:

Code:

 
static void Container_conInsExample(Args _args)
{
    // container declaration and initialization with 2 values
    container containerName = ["Welcome", 1202]; 
    str charHolder;
    ;
 
    // conIns() function usage 
    // (Inserts a value into a specific position in the container, index shifted next)
    // Note: Here Inserting value 299 at 2nd location and shifting the rest to the right!
    containerName = conIns(containerName, 2, 200);
    info(strFmt("Container containerName conIns() values are :- %1 , %2, %3", 
    conPeek(containerName, 1), conPeek(containerName, 2), conPeek(containerName, 3)));
}
 

Output:

The above code will produce the following result-

 
Container containerName conIns() values are :- Welcome , 200, 1202

 

 


·         The conFind() is used to find the first occurrence of an element or a sequence of elements in a container.

·         Syntax: int conFind (container container, anytype element,... )

o    container - The container to search.

o    element - One or more elements to search for, separated by commas.

·         Return value: Returns 0 if the item was not found; otherwise, the sequence number of the item.

Example:

Code:

 
static void Container_conFindExample(Args _args)
{
// container declaration and initialization with 2 values
    container containerName = ["Welcome", 1202]; 
    str charHolder;
    ;
 
    // conFind() function finds position in the container that a certain value is being held (if found)
    info(strFmt("conFind() - Find '1202' value in containerName container return if true(indexNo) else (0):- %1", conFind(containerName, 1202)));
    info(strFmt("conFind() - Find '2' value in containerName container return if true(indexNo) else (0):- %1", conFind(containerName, 2)));
}
 

Output:

The above code will produce the following result-

 
conFind() - Find '1202' value in containerName container return if true(indexNo) else (0):- 2
conFind() - Find '2' value in containerName container return if true(indexNo) else (0):- 0

 


·         The conLen() is used to retrieve the number of elements in a container.

·         Syntax: int conLen(container container)

o    container - The container in which to count the number of elements.

·         Return value: The number of elements in the container.

Example:

Code:

 
static void Container_conLenExample(Args _args)
{
// container declaration and initialization with 2 values
    container       containerName  =  ["Welcome", 1202, "good"]; 
    str             charHolder;
    ;
 
    // conLen() function returns the number of elements in the container
    // Here at this instance we have 2 elements []
    info(strFmt("containerName conLen() container length is :- %1", conLen(containerName)));
}
 

Output:

The above code will produce the following result-

 
containerName conLen() container length is :- 3

 


·         The conDel() is used to remove the specified number of elements from a container.

·         Syntax: container conDel(container container, int start, int number)

o    container - The container from which to remove elements.

o    start - The one-based position at which to start removing elements.

o    number - The number of elements to delete.

·         Return value: A new container without the removed elements.

Example:

Code:

 
static void Container_conDelExample(Args _args)
{
// container declaration and initialization with 2 values
    container containerName = ["Hello", 1202, 'good']; 
    str charHolder;
    ;
 
    // conDel() function removes a value from a specific position in the container.
    // Here removing 1 value starting from 2nd location
    containerName = conDel(containerName, 2, 1);
    info(strFmt("Container containerName conDel() values are :-  %1, %2", conPeek(containerName, 1), conPeek(containerName, 2)));
}
 

Output:

The above code will produce the following result-

 
Container containerName conDel() values are :-  Hello, good

 

 


·         The conNull() is used to retrieve an empty container. Use this function to explicitly dispose of the contents of a container.

·         Syntax: container conNull()

·         Return value: An empty container.

Example:

Code:

 
static void Container_conNullExample(Args _args)
{
    container containerName; // container declaration
    str charHolder;
    ;
    // container initialization with 4 values
    containerName = ["Finance", "T&L", "Retail", "Manufacturing"]; 
 
    // conNull() resets and returns an empty container
    // Here resets 4 values ("Finance", "T&L", "Retail", "Manufacturing") in containerName to null
    info(strFmt("Container containerName conNull() values before are :- %1 , %2, %3, %4",
    conPeek(containerName, 1), conPeek(containerName, 2),
    conPeek(containerName, 3), conPeek(containerName, 4)));
    
    containerName = conNull(); // set container null
    
    info(strFmt("Container containerName conNull() values after are :- %1 , %2, %3, %4",
    conPeek(containerName, 1), conPeek(containerName, 2),
    conPeek(containerName, 3), conPeek(containerName, 4)));
 
}
 

Output:

The above code will produce the following result-

 
Container containerName conNull() values before are :- Finance , T&L, Retail, Manufacturing
Container containerName conNull() values after are :- 0 , 0, 0, 0

 


·         This a Global class method which retrieves or displays a form with a visual representation of a container as a tree. A form with the visual representation of a container if the _lookup parameter was true; otherwise, null.

·         Syntax: client server public static FormRun conView(container containerToShow, [str _caption, boolean _lookup])

o    containerToShow - The container to show in the form.

o    caption - A caption to use on the form; optional.

o    lookup - true to return the form without displaying it; false to display the form and return null.

·         Return: A string of the elements in the container.

Example:

Code:

 
static void Container_conViewExample(Args _args)
{
    container containerName; // container declaration
    str charHolder;
    int conLength, cnt;
    ;
 
    // str2Con() function gets values str comma separated and converts to container
    // container initialization with 2 values using str2con()
    containerName = str2con("Technical,Functional, TechnoFunctional", ","); 
 
    // to view the results in tree form use conView() global function
    conView(containerName);
}
 

Output:

The above code will produce the following result-

 
Technical
Functional
TechnoFunctional

  

Comments