New object: an easy way to initialize an object

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:

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.

Fabrice Mainguené
• Product Owner •Fabrice Mainguené joined 4D Program team in November, 2016. As a Product Owner, he is in charge of writing the user stories then translating it to functional specifications. His role is also to make sure that the feature implementation delivered is meeting the customer need.After obtaining a Bachelor degree in Computer Science at CNAM, Fabrice joined a small software publishing company as a Windev developer. Then he worked for different companies in industry and trade areas as a Windev and web developer as well as technical advisor on new features.