ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

Help / WLanguage / OOP (Object Oriented Programming)
  • Copy rules
  • Case of arrays when copying class instances
  • Specific extension attributes
  • The Clone function
WINDEV
WindowsLinuxUniversal Windows 10 AppJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadIOS WidgetApple WatchMac CatalystUniversal Windows 10 App
Others
Stored procedures
Copy rules
Variables are copied using the "=" operator.
Type of variablesEffect
Simple types (integer, real, string, etc.)The value of the variable is copied.
ArraysThe destination array is a reference to the source array.
Associative arraysThe content of the array is copied.
QueueThe content of the queue is copied.
StackThe content of the stack is copied.
ListThe content of the list is copied.
Object = Dynamic objectThe members of the dynamic object are copied to the members of the object.
Object = ObjectThe members of the source object are copied to the members of the destination object.
Dynamic object = Dynamic objectThe destination dynamic object is a reference to the source dynamic object.
Dynamic object = ObjectThe destination dynamic object is a reference to the source object.
Structure = Dynamic structureA copy of the structure is performed. Members with the same name are initialized with the existing values. Non-existing members are ignored.
Structure = StructureA copy of the structure is performed. Members with the same name are initialized with the existing values. Non-existing members are ignored.
Dynamic structure = Dynamic structureThe destination dynamic structure is a reference to the source dynamic structure.
Dynamic structure = StructureThe destination dynamic structure is a reference to the source structure.
Advanced type = Advanced typeThe advanced type variable is copied. Properties of the same name are initialized with the existing values.
Advanced type = Dynamic advanced typeThe advanced type variable is copied. Properties of the same name are initialized with the existing values.
Dynamic advanced type = Advanced typeThe destination dynamic advanced variable is a reference to the source advanced variable.
Dynamic advanced type = Dynamic advanced typeThe destination dynamic advanced variable is a reference to the source dynamic advanced variable.

Case of arrays when copying class instances

When copying class instances, all the members of the class are copied to the new instance, including the arrays. Thus, arrays are independent in all class instances.
To prevent getting independent arrays in all class instances:
  1. Open the project description window: on the "Project" tab, in the "Project" group, click "Description".
  2. In the "Compilation" tab, uncheck "Arrays: the assignment copies the content".
For more details, see Project description, compilation tab.
Specific extension attributes
Two specific extension attributes can be used to indicate the operations to be performed:
  • <Copy=Clone>: when used on a dynamic object, this extension attribute allows forcing the cloning of the object.
  • <Copy=False>: when used on a member, this extension attribute prevents copying the member's value when copying an object to another object.
The Clone function
Clone is used to construct a copy of a class instance:
  • An object of the actual class of the class instance is allocated.
  • The members of the class instance are copied to the members of the new object. The "Constructor" and "Destructor" methods are also copied.
Benefit: In the case of polymorphism, there is no longer need to write the code necessary for this cloning. Likewise, there is no need to write virtual methods in all derived classes and constructors.
Remark: The copy takes into account the <Copy> attribute of each member:
  • <Copy=False> to ignore a member,
  • <Copy=Clone> to clone.
Example:
// There is a traffic light in North Phoenix, at 4725 E. Mayo Blvd.
// It is red
StopLight is TrafficLight
StopLight.Address = "4725 E. Mayo Blvd., Phoenix, AZ"
StopLight.Status = DarkRed
 
// Another traffic light must be set at 5599 on the same boulevard
 
// If the object is simply copied...
CopyLight is dynamic TrafficLight = StopLight
CopyLight.Address = "5599 E. Mayo Blvd., Phoenix, AZ"
// The address of StopLight has also changed...!
 
// If the object is cloned...
CloneLight is dynamic TrafficLight = Clone(StopLight)
// CloneLight contains the same information as StopLight
// But if one of it properties is modified, it does not affect the original traffic light
 
// Change the address
CloneLight.Address = "5599 E. Mayo Blvd., Phoenix, AZ"
// >> StopLight.Address = "4725 E. Mayo Blvd., Phoenix, AZ"
 
CloneLight.Status = DarkRed
// >> StopLight.Status = DarkGreen
Minimum version required
  • Version 25
This page is also available for…
Comments
Click [Add] to post a comment

Last update: 06/14/2023

Send a report | Local help