Home  |  Delphi .net Home  |  System.Collections.HashTable  |  Clone Method
Clone  
Method  
Make a new HashTable that is a clone of the current HashTable
HashTable Class
System.Collections NameSpace
CF1.  Function Clone ( ) : Object;
CF : Methods with this mark are Compact Framework Compatible
Description
A new HashTable is created with the same number of elements, attributes and key-value entries as the current HashTable.
 
When the current table holds reference (non primitive) data types, the cloned table element values still refer to the same objects that current table elements refer to. This is what is referred to as a shallow copy. A deep copy would create new versions of the referred objects.
Notes
You are obliged to cast the resultant Object to a HashTable before assignment to a HashTable variable.
Microsoft MSDN Links
System.Collections
System.Collections.HashTable
 
 
A simple example
program Project1;
{$APPTYPE CONSOLE}

uses
  System.Collections;

var
  Source, Target : System.Collections.HashTable;
  DictEntry      : DictionaryEntry;
  Enumerator     : IEnumerator;

begin
  // Create our hash tables
  Source := HashTable.Create;
  Target := HashTable.Create;

  // Add entries to the Source table
  Source['Name']     := 'Neil Moffatt';
  Source['Age']      := TObject(47);
  Source['Birthday'] := DateTime.Create(1957, 2, 18);

  // Display the Source table
  Console.WriteLine('Source table :');
  Console.WriteLine;
  Enumerator := Source.GetEnumerator;
  while Enumerator.MoveNext do
  begin
    DictEntry := DictionaryEntry(Enumerator.Current);
    Console.WriteLine('{0} = {1}',
                      DictEntry.Key.ToString,
                      DictEntry.Value.ToString);
  end;

  // Clone the Source table to the Target table
  Target := HashTable(Source.Clone);

  // Display the Target table
  Console.WriteLine;
  Console.WriteLine('Target table :');
  Console.WriteLine;
  Enumerator := Source.GetEnumerator;
  while Enumerator.MoveNext do
  begin
    DictEntry := DictionaryEntry(Enumerator.Current);
    Console.WriteLine('{0} = {1}',
                      DictEntry.Key.ToString,
                      DictEntry.Value.ToString);
  end;

  Console.Readline;
end.
Show full unit code
  Source table :
  
  Birthday = 18/02/1957 00:00:00
  Age = 47
  Name = Neil Moffatt
  
  Target table :
  
  Birthday = 18/02/1957 00:00:00
  Age = 47
  Name = Neil Moffatt
Illustrating the shallowness of the Clone method
program Project1;
{$APPTYPE CONSOLE}

uses
  System.Collections;

var
  Source, Target : System.Collections.HashTable;
  DictEntry      : DictionaryEntry;
  Enumerator     : IEnumerator;

begin
  // Create our hash tables
  Source := HashTable.Create;
  Target := HashTable.Create;

  // Add entries to the Source table
  Source['Name']     := 'Neil Moffatt';
  Source['Age']      := TObject(47);
  Source['Birthday'] := DateTime.Create(1957, 2, 18);

  // Display the Source table
  Console.WriteLine('Source table :');
  Console.WriteLine;
  Enumerator := Source.GetEnumerator;
  while Enumerator.MoveNext do
  begin
    // Note that entries are returned in key sorted order
    DictEntry := DictionaryEntry(Enumerator.Current);
    Console.WriteLine('{0} = {1}',
                      DictEntry.Key.ToString,
                      DictEntry.Value.ToString);
  end;

  // Clone the Source table to the Target table
  Target := HashTable(Source.Clone);

  // Display the Target table
  Console.WriteLine;
  Console.WriteLine('Target table :');
  Console.WriteLine;
  Enumerator := Source.GetEnumerator;
  while Enumerator.MoveNext do
  begin
    // Note that entries are returned in key sorted order
    DictEntry := DictionaryEntry(Enumerator.Current);
    Console.WriteLine('{0} = {1}',
                      DictEntry.Key.ToString,
                      DictEntry.Value.ToString);
  end;

  // Update the birthday value in just the Source
  Console.WriteLine;
  Console.WriteLine('Updating just the Source Birthday :');
  Console.WriteLine;
  Source['Birthday'] := DAteTime.Create(2000,1,1);

  // Display the Source table
  Console.WriteLine('Source table :');
  Console.WriteLine;
  Enumerator := Source.GetEnumerator;
  while Enumerator.MoveNext do
  begin
    // Note that entries are returned in key sorted order
    DictEntry := DictionaryEntry(Enumerator.Current);
    Console.WriteLine('{0} = {1}',
                      DictEntry.Key.ToString,
                      DictEntry.Value.ToString);
  end;

  // Display the Target table
  Console.WriteLine;
  Console.WriteLine('Target table :');
  Console.WriteLine;
  Enumerator := Source.GetEnumerator;
  while Enumerator.MoveNext do
  begin
    // Note that entries are returned in key sorted order
    DictEntry := DictionaryEntry(Enumerator.Current);
    Console.WriteLine('{0} = {1}',
                      DictEntry.Key.ToString,
                      DictEntry.Value.ToString);
  end;

  Console.Readline;
end.
Show full unit code
  Source table :
  
  Birthday = 18/02/1957 00:00:00
  Age = 47
  Name = Neil Moffatt
  
  Target table :
  
  Birthday = 18/02/1957 00:00:00
  Age = 47
  Name = Neil Moffatt
  
  Updating just the Source Birthday :
  
  Source table :
  
  Birthday = 01/01/2000 00:00:00
  Age = 47
  Name = Neil Moffatt
  
  Target table :
  
  Birthday = 01/01/2000 00:00:00
  Age = 47
  Name = Neil Moffatt
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author