Objects
Object Oriented Programming
Contents |
What about Receive objects (UDVs)?
When passing a string as parameter to a function or window for instance, you can specify as String or Receive String:
Function: MyFunction Parameters String: psParameter1 Receive String: rpsParameter2 Actions Set psParameter1 = "New string 1" Set rpsParameter2 = "New string 2"
The Receive String parameter can be changed inside the function.
There is no Receive for objects/UDV, how to change objects from within the function then?
Function: MyFunction Parameters cfcTest: puObject Actions Set puObject.sivName = "New string"
Well, in TD an UDV parameter is ALWAYS Receive, even when you do not specify it.
This means that in the sample code above, the puObject instance variable sivName is actually changed.
The functionality calling this function will have a changed object afterwards.
And no, there is not an option to prevent this.
It is possible to pass a Null-Object as a parameter to a function and create this object during the functions actions. So the caller of the function receives a new valid object for further use.
In C++ this technique is called "dereferencing".
Function: MyFunction Parameters cfcTest: puObject Actions Set puObject = new cfcTest Set puObject.sivName = "New string" Function: AnotherFunction Variables cfcTest: uObject = OBJ_Null Actions Call MyFunction( uObject ) Call SalMessageBox( uObject.sivName, "", MB_Ok )
But beware, the derefencing of UDV handle is not supported with window parameters. The internal architecture of passing parameters to functions is different from passing parameters to windows!
How to copy one object (instance) to another
Create a CopyFrom function in the class of the object (instance) which copies
all attributes from the source object to the destination object.
Functional Class: fcCustomer Instance Variables String: sivName Number: nivCustomerId Functions Function: CopyFrom Parameters fcCustomer: puSourceObject Actions Set sivName = puSourceObject.sivName Set nivCustomerId = puSourceObject.nivCustomerId
Having such a function in the class results in the following usage for instances.
Below you can see how to copy the contents (data) from the Customer1 object to Customer2.
After the Call, the two objects have the same data values.
Local variables fcCustomer: uCustomer1 fcCustomer: uCustomer2 Actions Call uCustomer2.CopyFrom( uCustomer1 )
To copy objects which contains nested objects, you can reuse the CopyFrom functions
of the nested UDV's like this
Functional Class: fcAdress Instance Variables String: sivStreet String: sivCity Functions Function: CopyFrom Returns Parameters fcAdress: puSourceObject Actions Set sivStreet= puSourceObject.sivStreet Set sivCity= puSourceObject.sivCity Functional Class: fcCustomer Instance Variables String: sivName Number: nivCustomerId fcAdress: uivAdress Functions Function: CopyFrom Returns Parameters fcCustomer: puSourceObject Actions Set sivName = puSourceObject.sivName Set nivCustomerId = puSourceObject.nivCustomerId Call uivAdress.CopyFrom( puSourceObject.uivAdress )
How to clear data of an object
1) Create a Clear function in the class of the object (instance) which sets all instance variables to initial values.
Functional Class: fcCustomer Instance Variables String: sivName Number: nivCustomerId Functions Function: Clear Returns Parameters Actions Set sivName = STRING_Null Set nivCustomerId = NUMBER_Null ! Below the code to clear an object Call uCustomer1.Clear( )
2) For TD versions offering the new statement
! Below the code to clear (initialise) an object Set uCustomer1 = new fcCustomer