This is the legacy 4D documentation web site. Documentations are progressively being moved to developer.4d.com

Home

 
4D v20 R7
New object

New object 


 

New object {( property ; value {; property2 ; value2 ; ... ; propertyN ; valueN} )} -> Function result 
Parameter Type   Description
property  Text in Name of property to create
value  Mixed in Value of property
Function result  Object in New language object

The New object command creates a new empty or prefilled object and returns its reference.

If you do not pass any parameters, New object creates an empty object and returns its reference. You must assign this reference to a 4D variable declared with _O_C_OBJECT or a 4D object field.

Note: _O_C_OBJECT declares a variable of the Object type but does not create any object.

Optionnally, you can prefill the new object by passing one or several property/value pairs as parameters:

  • In the property parameter, pass the label of the property to be created. Note that the property parameter is case sensitive. 
  • In the value parameter, pass the value you want to set for the property. Values of the following types are supported:
    • number (real, integer...) Number values are always stored as reals.
    • text
    • boolean
    • pointer
    • blob (4D.Blob)
    • date
    • time
    • null
    • picture
    • object
    • collection

Note that:

  • if you pass a pointer, it is kept as is; it will evaluated when using commands such as JSON Stringify,
  • dates are stored as "yyyy-mm-dd" dates or strings with the "YYYY-MM-DDTHH:mm:ss.SSSZ" format, according to the current "dates inside objects" database setting (see Compatibility page). When converting 4D dates into text prior to storing them in the object, by default the program takes the local time zone into account. You can modify this behavior using the Dates inside objects selector of the SET DATABASE PARAMETER command.
  • if you pass a time, it is stored as a number of milliseconds (Real).

This command can create empty of filled objects:

 C_OBJECT($obj1)
 C_OBJECT($obj2)
 C_OBJECT($obj3)
 $obj1:=New object
  // $obj1 = {}
 $obj2:=New object("name";"Smith")
  // $obj2 = {name:Smith}
 $obj3:=New object("name";"Smith";"age";40)
  // $obj3 = {name:Smith,age:40}

Creating a new object with an object as parameter value:

 C_OBJECT($Children;$Contact)
 
  //Creating an object array
 ARRAY TEXT($arrChildren;3)
 $arrChildren{1}:="Richard"
 $arrChildren{2}:="Susan"
 $arrChildren{3}:="James"
 OB SET ARRAY($Children;"Children";$arrChildren)
 
  //Initializing the object
 $Contact:=New object("FirstName";"Alan";"LastName";"Parker";"age";30;"Children";$Children)
  // $Contact = {FirstName:Alan,LastName:Parker,age:30,Children:{Children:[Richard,Susan,James]}}

This command is useful to pass objects as parameters:

 C_OBJECT($measures)
 $measures:=Get database measures(New object("path";"DB.cacheReadBytes";"withHistory";True;"historyLength";120))

With this command, you can easily handle objects in loops:

 ARRAY OBJECT($refs;0)
 C_LONGINT(vCounter)
 
 For(vCounter;1;100)
    APPEND TO ARRAY($refs;New object("line";"Line number "+String(vCounter)))
 End for



See also 

Download HDI database
New shared object

 
PROPERTIES 

Product: 4D
Theme: Objects (Language)
Number: 1471

This command can be run in preemptive processes

 
PAGE CONTENTS 
 
HISTORY 

Created: 4D v16 R3

 
ARTICLE USAGE

4D Language Reference ( 4D v20 R7)