Collection
Les collections sont des listes ordonnées de valeurs de types similaires ou différents (texte, nombre, date, objet, booléen, collection ou null).
Collection type variables are managed using object notation.
Pour accéder à un élément d'une collection, vous devez passer le numéro de l'élément entre crochets :
collectionRef[expression]
You can pass any valid 4D expression which returns a positive integer in expression. Exemples :
myCollection[5] //access to 6th element of the collection
myCollection[$var]
Warning: Collection elements are numbered from 0.
Vous pouvez assigner une valeur à un élément de collection ou lire une valeur d'élément de collection :
myCol[10]:="My new element"
$myVar:=myCol[0]
Si vous assignez un numéro d'élément plus grand que celui du dernier élément existant dans la collection, la collection est automatiquement redimensionnée et les nouveaux éléments intermédiaires prennent la valeur null :
var myCol : Collection
myCol:=New collection("A";"B")
myCol[5]:="Z"
//myCol[2]=null
//myCol[3]=null
//myCol[4]=null
Instanciation
Les collections doivent avoir été instanciées, sinon toute tentative de lecture ou de modification de leurs éléments génère une erreur de syntaxe.
L'instanciation d'une collection peut se faire de l'une des manières suivantes :
- using the
New collection
command, - using the
[]
operator.
Several 4D commands and functions return collections, for example Get Monitored Activity
or collection.copy
. Dans ce cas, il n'est pas nécessaire d'instancier explicitement la collection, le langage 4D le fait pour vous.
New collection
command
The New collection
command creates a new empty or prefilled collection and returns its reference.
Exemples :
var $colVar : Collection //declaration of a collection type 4D variable
$colVar:=New collection //instantiation of the collection and assignment to the 4D variable
var $colFilled : Collection
$colFilled:=New collection("a";"b";1;42;{}) //instantiation and assignment of a prefilled collection
[]
operator
The []
operator allows you to create a collection literal. A collection literal is a list of zero or more expressions, each of which represents a collection element, enclosed in square brackets ([]
). Lorsque vous créez une collection en utilisant une collection littérale, elle est instanciée avec les valeurs spécifiées comme éléments, et sa longueur est définie au nombre d'arguments spécifiés.
Since any element is considered an expression, you can create sub-collections using []
in elements. You can also create and reference object literals.
Si un élément n'est pas défini, il apparaîtra comme Null dans la collection.
Exemples :
var $col1; $col2; $users : Collection
$col1:=[] //empty collection
$col2:=[1;2;3;4;5;6] //collection of numbers
//collection of objects
$users:=[{name: "Alice"; \
height: 183; \
eyecolor: "hazel"; \
id: $col2[5]\
}; \
{name: "Bob"; \
height: 172; \
eyecolor: "blue"\
}]
If you create a collection literal containing a single element, make sure you do not use a name corresponding to an existing table name, otherwise the table syntax [tableName]
will take priority.
Collection standard ou collection partagée
Vous pouvez créer deux types de collections :
- regular (non-shared) collections, using the
New collection
command or collection literal syntax ([]
). Ces collections peuvent être modifiées sans contrôle d'accès spécifique mais ne peuvent pas être partagées entre les process. - shared collections, using the
New shared collection
command. Le contenu de ces collections peut être partagé entre les process, y compris des process (thread) préemptifs. Access to these collections is controlled byUse...End use
structures.
For more information, refer to the Shared objects and collections section.
Fonctions de collection
4D collection references benefit from special class functions (sometimes named member functions). Collection functions are listed in the Class API Reference section.
Par exemple :
$newCol:=$col.copy() //copie de $col vers $newCol
$col.push(10;100) //ajout de 10 et 100 à la collection
Certaines fonctions retournent la collection d'origine après modification, de manière à ce que vous puissiez enchaîner les appels dans une même séquence :
$col:=New collection(5;20)
$col2:=$col.push(10;100).sort() //$col2=[5,10,20,100]
paramètre propertyPath
Several functions accept a propertyPath as parameter. Ce paramètre peut contenir :
- soit un nom de propriété d'objet, par exemple "nomComplet"
- soit un chemin de propriété d'objet, c'est-à-dire une séquence hiérarchique de sous-propriétés reliées par des points, par exemple "employé.enfant.prénom".
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:
$vmin:=$col.min("My.special.property") //undefined
$vmin:=$col.min(["My.special.property"]) //error
Undefined
Reading the length property of an undefined collection produces 0:
var $c : Collection //variable created but no collection is defined
$size:=$c.length //$size = 0