KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Distinguishing the assignment operator and OB Copy when copying a 4D Object.
PRODUCT: 4D | VERSION: 14.x | PLATFORM: Mac & Win
Published On: October 21, 2015

Programming in 4D at a given point would require using the assignment operator ":=" which copies the value of the expression to the right of the assignment operator into the variable or field to the left of the operator. An example would be variable types like text and numbers which copies on the left of the operator shown below:

C_LONGINT($value1;$value2)
C_TEXT($text1;$text2)

$value1:=23
$value2:=$value1 // $value2=23, $value1=23
$value2:=11      // $value2=11, $value1=23

$text1:="ABC"
$text2:=$text1   // $text2="ABC", $text1="ABC"
$text2:="DEF"    // $text2="DEF", $text1="ABC"


But how about using the assignment operator with an 4D Object? 4D uses the assignment operator differently with 4D Object by passing the reference to the object variable on the left of the operators. If either objects are modified, both will be the same copy. An example is shown below:

C_OBJECT($obj1;$obj2)

OB SET($obj1;"Data1";"ABC";"Data2";"DEF")

$obj2:=$obj1 // $obj1={"Data1":"ABC","Data2":"DEF"}
             // $obj2={"Data1":"ABC","Data2":"DEF"}

OB SET NULL($ob1;"Data"2) // $obj1={"Data1":"ABC","Data2":null}
                          // $obj2={"Data1":"ABC","Data2":null}


To have two separate entities of the copied object, OB COPY can be used as shown below:

OB SET($obj1;"Data1";"ABC";"Data2";"DEF")

$obj2:=OB Copy($obj1) // $obj1={"Data1":"ABC","Data2":"DEF"}
                      // $obj2={"Data1":"ABC","Data2":"DEF"}

OB SET NULL($obj1;"Data2") // $obj1={"Data1":"ABC","Data2":null}
                           // $obj2={"Data1":"ABC","Data2":"DEF"}


Using the assignment operator can be used for a 4D Object type as a straight copy in which the original object used for copying might not be reused in the program. But there are some cases for instance a loop where the original 4D object would need to be repeatedly copied from and that is where the data is inaccurate. It is best practice to use OB GET to isolate each copy of the 4D object.

See Also: