Tech Tip: Activate dot notation in order to use bracket syntax for collections
PRODUCT: 4D | VERSION: 16R4 | PLATFORM: Mac & Win
Published On: August 1, 2017
4D v16R4 introduces a new variable type called Collection. A Collection is sort of like an array in that it is a collection of data, but unlike an array which is not limited to a single data type. Collection elements are accessed via [ and ] brackets and numbered starting at 0, unlike an array which uses { and } brackets and numbered starting at 1.
Here is a quick example of creating a collection:
// setup collection C_COLLECTION($vc_Temp) $vc_Temp:=New collection(1;True;"tree") |
The example above places the following Collection into the $vc_Temp variable:
[1,true,"tree"] |
In order to utilize the bracket syntax you must enable dot notation:
After enabling this setting then the bracket syntax will work:
// use bracket syntax to access collection data C_LONGINT($vl_Temp) C_BOOLEAN($vb_Temp) C_TEXT($vt_Temp) $vl_Temp:=$vc_Temp[0] // first element, longint (1) $vb_Temp:=$vc_Temp[1] // second element, boolean (True) $vt_Temp:=$vc_Temp[2] // third element, text ("Three") |
Attempting to use bracket syntax without activating dot notation will result in incorrect values being returned.
Without enabling this feature, it is possible to use the $vc_Temp collection with various Object commands, such as the following:
This $vc_Temp collection can be used with JSON STRINGIFY to see the contents:
ALERT(JSON Stringify($vc_Temp)) |
This $vc_Temp collection can also be used with the OB SET command like this:
// use collection in object C_OBJECT($vo_Temp) $vo_Temp:=New object("test";$vc_Temp) |