Clone Method (For variable based on SDT which is collection)

Official Content
This documentation is valid for:
Creates a new memory area and copies the variable data on it.

Syntax

Var1 = Var2.Clone()

Where:
Var1, Var2
    Are variables based on SDT

Description

It must always be assigned to a variable; the only exception is within the Add command; i.e.: 
   &var1.add(var2.clone())

Clone Method vs New Operator

The Clone method is equivalent to a variable new and the assignment of each variable item; i.e.: 
Z = Y.clone()  → Z = New SDT()
                             z.a = y.a
                             z.b = y.b

Where ‘a’ and ‘b’ are elements of the structured SDT.

Note that the assignment Z = Y simply redirects both variables to the same memory area, and both variables are modified just by modifying one. 

The advantage of the clone method as regards the new is that it makes an automatic copy of each SDT item and there is no need to make the assignment. 

If there are substructures within the SDT (be them collections or not), a new instance of the substructure is not created with the clone method (shallow copy). You must do an explicit new or clone for each substructure. If there are “defined” substructures within the SDT that are not collections, the clone method is not enabled.

Example with the Country SDT:

Name    Char
Continent
    Name   Char

&Country.Name = 'Uruguay' 
&Country.Continent.Name = 'América'
&Countries.Add(&Country.clone())

&Country.Nombre = 'Senegal' 
&Country.Continent.Name = 'Africa'
&Countries.Add(&Country.clone())
Where &Country is based on Structured Data Type Country, and &Countries it’s a collection of Country. In both countries the continent is Africa. If you want each record with a different continent, you must program this with the new operator.

Scope

Objects: Procedures, Transactions, Web Panels
Languages: .NET, Java, Ruby (up to GeneXus X Evolution 3), Visual FoxPro (up to GeneXus X Evolution 3)
Interfaces: Web

See Also

Add Method
New operator (SDT)
Structured Data Type (SDT) object