Discover New object, the new command of 4D to initialize an object. This command allows you to either create an empty object or create it with some initial properties and values. OB SET is now becoming unnecessary in several situations, New object is enough.
New object also allows to re-initialize an temporary object in a loop or simply directly pass an object as a parameter to a 4D command requesting an object as parameter, as Get database measures or GRAPH commands for instance.
This new command changes your way to write 4D code in many situations. New object creates an object and returns a reference to it. It allows more flexibility in your code. This command is the first of a series of new features related to objects … Stay tuned !
Example 1
This first example shows you how you can just create an empty object. In the previous 4D versions, the only way to do this was to use JSON Parse(“{}”), which is not obvious. In fact the C_OBJECT command declares an object variable that doesn’t contain any object and the object is actually created only at the first OB SET call as well as some properties are defined. From v16 R3 no more need to use JSON Parse, there is a single command to initialize your objects: New object! Easy and intuitive!
// Previous versions
C_OBJECT($obj)
$obj:=JSON Parse("{}")
//$obj={}
Since 4D v16 R3 you can just use New object:
// From 4D v16 R3
C_OBJECT($obj)
$obj:=New object
//$obj={}
Example 2
You can also directly use New object inside a 4D command without creating an object before as shown below.
// Previous versions
C_OBJECT($oParams)
C_OBJECT($measures)
OB SET($oParams;"path";"DB.cacheReadBytes")
OB SET($oParams;"withHistory";True)
OB SET($oParams;"historyLength";2*60)
$measures:=Get database measures($oParams)
Since 4D v16 R3 you can write this code using New object :
// From 4D v16 R3
C_OBJECT($measures)
$measures:=Get database measures(New object("path";"DB.cacheReadBytes"; "withHistory";True;"historyLength";120))
You can find more code examples in the following example database:
For more details, you can also refer to the documentation. Just take a look at the article dedicated to the New object command in the 4D v16 R3 language manual.