| Description |
A new SortedList is created with the same number of elements, attributes and key-value entries as the current SortedList.
When the current list holds reference (non primitive) data types, the cloned list element values still refer to the same objects that current list 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 SortedList before assignment to a SortedList variable.
|
|
| Microsoft MSDN Links |
System.Collections
System.Collections.SortedList
|
|
|
| A simple example |
program Project1;
{$APPTYPE CONSOLE}
uses
System.Collections;
var
Source, Target : System.Collections.SortedList;
DictEntry : DictionaryEntry;
Enumerator : IEnumerator;
begin
// Create our sorted lists
Source := SortedList.Create;
Target := SortedList.Create;
// Add entries to the Source list
Source['Name'] := 'Neil Moffatt';
Source['Age'] := TObject(47);
Source['Birthday'] := DateTime.Create(1957, 2, 18);
// Display the Source list
Console.WriteLine('Source list :');
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 list
Target := SortedList(Source.Clone);
// Display the Target list
Console.WriteLine;
Console.WriteLine('Target list :');
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 :
Age = 47
Birthday = 18/02/1957 00:00:00
Name = Neil Moffatt
Target table :
Age = 47
Birthday = 18/02/1957 00:00:00
Name = Neil Moffatt
| | | Illustrating the shallowness of the Clone method | program Project1;
{$APPTYPE CONSOLE}
uses
System.Collections;
var
Source, Target : System.Collections.SortedList;
DictEntry : DictionaryEntry;
Enumerator : IEnumerator;
begin
// Create our sorted lists
Source := SortedList.Create;
Target := SortedList.Create;
// Add entries to the Source list
Source['Name'] := 'Neil Moffatt';
Source['Age'] := TObject(47);
Source['Birthday'] := DateTime.Create(1957, 2, 18);
// Display the Source list
Console.WriteLine('Source list :');
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 list
Target := SortedList(Source.Clone);
// Display the Target list
Console.WriteLine;
Console.WriteLine('Target list :');
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;
// 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 list
Console.WriteLine('Source list :');
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 list
Target := SortedList(Source.Clone);
// Display the Target list
Console.WriteLine;
Console.WriteLine('Target list :');
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 list :
Age = 47
Birthday = 18/02/1957 00:00:00
Name = Neil Moffatt
Target list :
Age = 47
Birthday = 18/02/1957 00:00:00
Name = Neil Moffatt
Updating just the Source Birthday :
Source list :
Age = 47
Birthday = 01/01/2000 00:00:00
Name = Neil Moffatt
Target list :
Age = 47
Birthday = 01/01/2000 00:00:00
Name = Neil Moffatt
|
| |
|
|
|