Tech Tip: Copying an Object from one variable to another
PRODUCT: 4D | VERSION: 15.x | PLATFORM: Mac & Win
Published On: July 27, 2017
When trying to duplicate an object variable and using the ": =" operator the two objects variables are going to be sharing the same reference. A modification to either object variables will have an impact to the same object in the memory. Here is an example:
C_OBJECT($ref_richard;$Object) ARRAY OBJECT($arrayChildren;0) OB SET($ref_richard;"name";"Richard";"age";7) APPEND TO ARRAY($arrayChildren;$ref_richard) $Object:=$ref_richard OB SET($Object;"name";"sara";"age";26) APPEND TO ARRAY($arrayChildren;$Object) |
The result is :
To assign a duplicate copy of an object from one variable to another, use the command OB COPY. Here is a modification of the code shown above, using the OB COPY command.
C_OBJECT($ref_richard;$Object) ARRAY OBJECT($arrayChildren;0) OB SET($ref_richard;"name";"Richard";"age";7) APPEND TO ARRAY($arrayChildren;$ref_richard) $Object:=OB Copy($ref_richard) OB SET($Object;"name";"sara";"age";26) APPEND TO ARRAY($arrayChildren;$Object) |
The result is :