Home  |  Delphi .net Home  |  System.Object  |  MemberwiseClone Method
  
MemberwiseClone  
Method  
Performs a shallow copy of the current object to a new object
Object Class
System NameSpace
CF1.  Function MemberwiseClone : Object;
CF : Methods with this mark are Compact Framework Compatible
Description
The MemberwiseClone method can only be used within a class. It allows the class to clone the contents of an instance of itself to a new object.
 
The clone is what is called a shallow one - all members are copied, but members that are references do not have their referenced objects cloned.
 
The consequence of this is that the new object will still literally refer to these referenced objects exactly as the current object does. Any changes to the referenced objects affects both the current and cloned objects.
Microsoft MSDN Links
System
System.Object
 Author links

 Buy Website Traffic at
 Buywebsitetrafficexperts.com

 Buy Proxies at
 Buyproxies.io
 
 
 
Illustrating MemberwiseClone in a simple new class
program Project1;
{$APPTYPE CONSOLE}

type
  // Class containing an array of strings
  ArrayClass = Class(System.Object)
    private
      StrArray : Array of String;

    published
      Constructor Create;

      Procedure   ListArray;
      Procedure   SetStrings(S0:String; S1:String; S2:String);
      Function    ShallowClone : System.Object;
  end;

// Class methods
constructor ArrayClass.Create;
begin
  inherited;

  // Allocate the array
  SetLength(StrArray, 3);

  // Assign array string values
  StrArray[0] := 'Hello';
  StrArray[1] := 'cruel';
  StrArray[2] := 'World';
end;

procedure ArrayClass.ListArray;
var
  i : Integer;
begin
  for i := 0 to Length(StrArray)-1 do
    Console.WriteLine(StrArray[i]);
end;

procedure ArrayClass.SetStrings(S0, S1, S2: String);
begin
  StrArray[0] := S0;
  StrArray[1] := S1;
  StrArray[2] := S2;
end;

function ArrayClass.ShallowClone : System.Object;
begin
  // MemberwiseClone is only accessible within the class
  Result := MemberwiseClone;
end;


// Main code
var
  Obj1, Obj2 : ArrayClass;

begin
  // Instantiate the first object
  Obj1 := ArrayClass.Create;

  // Shallow clone to another object
  Obj2 := ArrayClass(Obj1.ShallowClone);

  // Display the class contents
  Console.WriteLine('Obj1 strings :');
  Obj1.ListArray;
  Console.WriteLine;

  Console.WriteLine('Obj2 strings :');
  Obj2.ListArray;
  Console.WriteLine;

  // Update the first object strings
  Obj1.SetStrings('Updated', 'The', 'Strings');

  // Display the class contents
  Console.WriteLine('Obj1 strings now :');
  Obj1.ListArray;
  Console.WriteLine;

  Console.WriteLine('Obj2 strings now :');
  Obj2.ListArray;
  Console.WriteLine;

  Console.WriteLine('See that the objects refer to the same strings!');
  Console.WriteLine('This is the consequence of a shallow clone');

  Console.ReadLine;
end.
   Obj1 strings :
   Hello
   cruel
   World
  
   Obj2 strings :
   Hello
   cruel
   World
  
   Obj1 strings now :
   Updated
   The
   Strings
  
   Obj2 strings now :
   Updated
   The
   Strings
  
   See that the objects refer to the same strings!
   This is the consequence of a shallow clone
 
 
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Contact the author