Wednesday, May 30, 2012

Microsoft® DynamicsTM Ax container manipulation using X++

Note: There is no warranty in the code written below. Use at your own risk.

Overview
Container is a built-in data type, X++ supports a number of functions to manipulate the contents of containers. This topic describes the built-in functions for manipulating containers. In other way we can say X++ has a general data type called a container that can be regarded as a dynamic untyped array of primitive data types, containers, and arrays. Containers can be used to hold a list of items of different types.

Allocation of containersContainers are automatically allocated, when a container variable is used which means that you do not have to create a container using new.
Disposal of containers
To explicitly dispose the contents of a container use the connull function.
void DisposalMethod()
{
container con;
con = connull( );

Length
When you operate on a container, you often need to know how many items it contains. The conlen function returns the number of items in the container.
void LengthMethod()
{
container con;
//gives output 0 as there are initially no elements in a container
print conlen(con);

Finding a sequence of elements
To locate a sequence of items in a container, use the confind function. confind takes a container and one or more items as arguments. The function returns 0 (the item was not found) or the item’s sequence number.
void SequenceMethod()
{
container con = [100,110, ”Hello World”];
int i;
i = confind( con, “Hello World”); //i has the value of Hello World
}

Insertion
Sometimes you need to insert some items in a container. Use the conins function, which takes a variable number of arguments: the container, position, and the items to be inserted.
void InsertionMethod()
{
container con;
con = conins(con,10,”Hello Testing”);
}


Deletion
To delete one or more items from a container, use the condel function. condel takes three arguments: the container, the start position for deletion, and the number of items to be deleted.
void DeletionMethod()
{
container con = ["Hello", 100, 5.55);
con = condel(con,1,2);
}


Replacement
To replace an item in a container, use the conpoke function. conpoke takes a variable number of arguments: the container, the position (to be poked), and the new item.
void ReplacementMethod()
{
container con = [10, 30.11, ”Hello”];
// Replaces the first item - 10 - with “Hello World”
con = conpoke(con, 1, ”Hello World”);
}

Extraction
A container allows you to hold a number of items, possibly of different types, and then use them in some other context. To use the items, you need to be able to extract them from the container. This is the purpose of the conpeek function. conpeek takes two arguments: the container and the position to be peeked.


The conpeek function automatically converts the peeked item into the expected return type. Strings can automatically be converted into integers and vice versa.
void ExtractionMethod() 
{
str 10 string;
container con = [10, 30.11, ”Good Testing”];
// Extracts the real 30.11 and converts it into a string: “30.11”
string = conpeek(con,2);
}


You can extract more than one element from a container with a composite assignment:
void ExtractionMethod1()
{
str 10 string;
int i;
real rl;
container con = [10,30.11,”Hello”];
[il,r, string] = con; // Extracts 10 into i, 30.11 into rl and “Hello” into string

}

No comments:

How to identify the user that was used to change an object from AOT in AX2012

Get the object name for which we need to track these (user and date&time) information's. Login to SQL Server Management Studio an...