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).
Pour manipuler les variables de type Collection, vous devez utiliser la notation objet (voir Les bases de la syntaxe).
Pour des informations complémentaires sur les collections 4D, passez le numéro (l'indice) de l'élément entre crochets :
collectionRef[expression]
Vous pouvez passer toute expression 4D valide qui retourne un nombre entier positif dans expression. Exemples :
myCollection[5] //accès au 6e élément de la collection
myCollection[$var]
Attention : N'oubliez pas que la numérotation des éléments de collection débute à 0.
Vous pouvez assigner une valeur à un élément de collection ou lire une valeur d'élément de collection :
myCol[10]:="Mon nouvel élément"
$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
Collections must have been instantiated, otherwise trying to read or modify their elements will generate a syntax error.
Collection instantiation can be done in one of the following ways:
- using the
New collection
command, - using the
[]
operator.
Several 4D commands and functions return collections, for example Get Monitored Activity
or collection.copy
. In this case, it is not necessary to instantiate explicitely the collection, the 4D language does it for you.
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 ([]
). When you create a collection using a collection literal, it is instantiated with the specified values as its elements, and its length is set to the number of arguments specified.
Since any element is considered an expression, you can create sub-collections using []
in elements. You can also create and reference object literals.
If an element is undefined, it will appear as Null in the 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. - collections partagées, à l'aide de la commande
New shared collection
. Le contenu de ces collections peut être partagé entre les process, y compris des process (thread) préemptifs. L'accès à ces collections doit être contrôlé via des structuresUse...End use
.
Pour plus d'informations, consultez la section Objets et collections partagés.
Fonctions de collection
Les références de collections 4D bénéficient de fonctions de classe spécifiques (souvent appelées fonctions méthodes). 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 cheminPropriété
Plusieurs fonctions admettent un paramètre nommé cheminPropriété. 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") //indéfini
$vmin:=$col.min(["My.special.property"]) //erreur