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.
The elements of a container can be any combination of valid types, in any order, including nested containers. A number of functions are provided to manipulate the values inside a container.
Containers are dynamic and have no limits. They can contain elements of almost all data types: boolean, integer, real, date, string, container, arrays, tables, and extended data types. However, objects cannot be stored in containers.
Note |
|---|
|
Variables in objects and fields in tables can be declared as containers.
|
Declaring and initializing containers
|
container declaration
|
=
|
container Variable { , Variable } ;
|
|
Variable
|
=
|
Identifier [ option ]
|
|
Option
|
=
|
Arrayoptions | initialization
|
// Declaration of a container, c1
container c1;
// A dynamic array of containers is declared
container c2[];
// A container is declared and initialized with three items:
// an integer (1), a real (3.14), and a string "a".
container c3 = [1,3.14,"a"];
Note |
|---|
|
You cannot add more than 40 elements to a container at one time. You can add additional elements as shown in the following example.
c3 += [23,34,55,62,73];//additional elements
You may use the += operator to add values of any type into a container. For example, to create a container containing the first ten square numbers:
int i;
container c;
;
for (i = 1; i <= 10; i++)
{
c += i+i;
}
|
Although containers are a list of values, you can specify literals for them. Use the square brackets around the elements in the container:
container c = [1,3.14,"abc"];
When you use built-in functions to manipulate containers, the elements are automatically converted into the result type. The following example inserts a string and a real, respectively, into a container, and then fetches the items again but in reverse order. The string is fetched into a real and the real is fetched into a string.
real r = 0;
str 30 s = "";
container c = ["3.14",3.14];
;
s = conpeek(c,2);
r = conpeek(c,1);
The result is that s = "3.14" and r = 3.14. The conpeek function has converted the item to the correct type. For more information, see conPeek Function.
|
Keyword
|
container
|
|
Size of data type
|
Dynamic
|
|
Scope of data type
|
User-defined
|
|
Default value
|
0
|
|
Implicit conversions
|
None
|
|
Explicit conversions
|
None
|
Struct Class