Collections are ordered lists of values of similar or mixed types (text, number, date, object, boolean, collection, or null).
Collection type variables are managed using object notation (see Syntax basics).
To access a collection element, you need to pass the element number inside square brackets:
You can pass any valid 4D expression which returns a positive integer in expression. Examples:
myCollection //access to 6th element of the collection
Warning: Collection elements are numbered from 0.
You can assign a value to a collection element or get a collection element value:
myCol:="My new element"
If you assign an element's index that surpasses the last existing element of the collection, the collection is automatically resized and all new intermediary elements are assigned a null value:
var myCol : Collection
Collections must have been initialized, for example using the
New collection command, otherwise trying to read or modify their elements will generate a syntax error.
var $colVar : Collection //creation of collection type 4D variable
$colVar:=New collection //initialization of the collection and assignment to the 4D variable
Regular or shared collection
You can create two types of collections:
- regular (non-shared) collections, using the
New collectioncommand. These collections can be edited without any specific access control but cannot be shared between processes.
- shared collections, using the
New shared collectioncommand. These collections can be shared between processes, including preemptive threads. Access to these collections is controlled by
For more information, refer to the Shared objects and collections section.
4D collection references benefit from special class functions (sometimes named member functions). Collection functions are listed in the Class API Reference section.
$newCol:=$col.copy() //deep copy of $col to $newCol
$col.push(10;100) //add 10 and 100 to the collection
Some functions return the original collection after modification, so that you can run the calls in a sequence:
Several functions accept a propertyPath as parameter. This parameter stands for:
- either an object property name, for example "lastName"
- or an object property path, i.e. a hierarchical sequence of sub-properties linked with dot characters, for example "employee.children.firstName".
Warning: When using functions and propertyPath parameters, you cannot use ".", "[ ]", or spaces in property names since it will prevent 4D from correctly parsing the path: