Objects are great, everyone says so. Beside their wonderful flexibility, they are unbelievably fast. You can search through millions of records in just a fraction of a second. 4D v16 even allows to order by object attributes, and to do calculations such as Sum or Average.
With 4D v16 R4, the 4D language has been drastically enhanced to make the usage of objects much more comfortable – with the support of the object notation. Fast, flexible, efficient and now elegant!
What’s possible?
4D language objects can be handled using the standard object notation to get or to set their values. With object notation:
- object properties can be accessed using a “dot” symbol (e.g. employee.name)
- object collection elements can be accessed using square brackets (e.g. rooms[2])
- embedded sub-objects and sub-object properties can be accessed through a sequence of symbols (e.g. employee.children[2].age).
Good news, now you have a mean to get rid of all those OB Get and OB SET in your code! And you know what? The code execution is 10 times faster with the object notation compared to OB Get/OB SET usage!
Examples of object notation
Let’s say we have a $o object containing the following information:
{ "Lastname": "Doe", "Firstname": "John", "Avatar": "[object Picture]" "Child": [{ "Name": "Susan", "Age": 3 } , { "Name": "Bob", "Age": 8 }] }
To retrieve the “Firstname” attribute, you write:
With object notation
C_TEXT($name)
$name:=$o.Firstname
Without object notation
C_TEXT($name)
$name:=OB Get($o;"Firstname")
To retrieve the “Name” of the second children, you write:
With object notation
C_TEXT($name)
$name:=$o.Child[1].Name
Without object notation
ARRAY OBJECT($aChild;0)
C_TEXT($name)
OB GET ARRAY($o;"Child";$aChild)
$name:=OB GET($aChild{2};"Name")
To add another children, you write:
With object notation
$o.Child[2].Name:="Mike"
$o.Child[2].Age:=9
Without object notation
ARRAY OBJECT($aChild;0)
C_OBJECT($kid)
OB GET ARRAY($o;"Child";$aChild)
OB SET($kid;"Name";"Mike")
OB SET($kid;"Age";9)
APPEND TO ARRAY($aChild;$kid)
OB SET ARRAY($o;"Child";$aChild)
To add an image in an object attribute, you write:
With object notation
C_PICTURE($img)
READ PICTURE FILE("img.jpg";$img)
$o.Avatar:=$img
Without the object notation
C_PICTURE($img)
READ PICTURE FILE("img.jpg";$img)
OB SET($o;"avatar";$img)
Read more about Pictures in Objects >
To use pointer notation, you write:
With object notation
C_POINTER($p)
$p:=->$o
...
C_TEXT($name)
$name:=$p->Firstname
Without object notation
C_POINTER($p)
$p:=->$o
...
C_TEXT($name)
$name:=OB Get($p->;"Firstname")
How do I enable object notation?
To be able to benefit from the object notation, you must go to the Compatibility Settings page in Database Settings dialog and click on the “Activate object notation” button:
There are some prerequisites before activating object notation, but we are helping you to prepare: