Objects Corner: easy sharing & other good news

Automatically translated from English

How many times have you gotten the “Not supported value type in a shared object” error when trying to put an object into a shared object?

What did you do? Write longer code to turn your innocent object into a shared one, perhaps iterating over all of its properties?

How about transferring a shared object from one shared group to another?


An interesting thing about objects: you already know they can be used as a hash map (a key/value system), but what about gettings all the keys or all the values with a single line of code?

Keep reading, because 4D v18 R3 is out and brings good news!

COPY shared objects and collections

copy an object as shared

The OB Copy() command has been enhanced to accept a new option which can have the value: ck shared 

In the example below, the $person object is copied as a shared object into the $copy object.

Then, we can put the $copy object into the $personsList shared object.

$text:=Document to text(Get 4D folder(Current resources folder)+"person.txt")
//$person is a standard object
$person:=JSON Parse($text)
$personsList:=New shared object()
//$copy is a shared object
$copy:=OB Copy($person;ck shared)
Use ($personsList)
//We can put $copy in $personsList because $copy is a shared object
End use

assign a shared object from a shared group to another

The OB Copy() command has also been enhanced to accept a new parameter: groupWith (which must be a shared object or a shared collection).

Passing the groupWith parameter allows putting the copied object into the same shared group as groupWith.

In the example below, $sharedObj and $sharedColl belong to two separate shared groups.

We can’t put $sharedObj into $sharedColl without getting an error, but we can easily copy $sharedObj into a new object belonging to the same shared group as $sharedColl.

//$sharedObj belongs to a shared group

$sharedObj:=New shared object("lastname";"Smith";"address";New shared object("city";"New York"))
//$sharedColl belongs to another shared group

$sharedColl:=New shared collection(New shared object("lastname";"Brown"))
//$objCopy is in the same shared group as $sharedColl

$objCopy:=OB Copy($sharedObj;ck shared;$sharedColl)
//So we can put $objCopy into $sharedColl without error

Use ($sharedColl)
End use


and what about shared collections?

The examples above are related to the OB Copy() command.

The collection.copy() member method has been enhanced in the exact same way.

handle an object as a hash map

There are new commands to make handling an object as a hash map easier. If you have objects whose property names contain dynamic data, these commands are for you!

Let’s explain this with a single example.

Consider this $persons object containing firstnames as a property name and age as a property value:

$persons:=New object
$persons["John"]:=42 // John is 42
$persons["Andy"]:=24 // Andy is 24
$persons["Mary"]:=30 // Mary is 30
$persons["Paul"]:=50 // Paul is 50
$persons["Fred"]:=51// Fred is 51

OB Keys command

This command returns the property names of an object as a collection:

ALERT("There are "+String(OB Keys($persons).length)+" persons")

OB Values command

This command returns the property values of an object as a collection:

ALERT("The age average is "+String(OB Values($persons).average()))

OB Entries command

This command returns a collection of objects with key properties (property name) and value (property value):

$agesOK:=OB Entries($persons).query("value>:1";45)
ALERT("There are "+String($agesOK.length)+" persons who are over 45")
ALERT("Their names are: "+$agesOK.extract("key").join("-"))

Using an object as shown above, provides quick, direct access to data like when using an index.

It’s faster than using a collection of objects with firstname and age properties.

Also note that those commands return a collection. This allows us to use all the member methods available on collections (like average(), query(), …).




• Product Owner •

Marie-Sophie Landrieu-Yvert has joined the 4D Product team as a Product Owner in 2017. As a Product Owner, she is in charge of writing the user stories then translating it to functional specifications. Her role is also to make sure that the feature implementation delivered is meeting the customer need.

Marie-Sophie graduated from the ESIGELEC Engineering School and began her career as an engineer at IBM in 1995. She participated on various projects (maintenance or build projects) and worked as a Cobol developer. Then she worked as an UML designer and Java developer. Lately her main roles were analyzing and writing functional requirements, coordinate business and development teams.