Home  |  Delphi .net Home  |  System.Collections.ArrayList  |  SetRange Method
  
SetRange  
Method  
Overlays elements from a collection onto the current ArrayList
ArrayList Class
System.Collections NameSpace
NotCF1.  Procedure SetRange ( TargetIndex : Integer; SourceCollection : ICollection ) ;
CF : Methods with this mark are Compact Framework Compatible
Description
The SetRange method takes elements from SourceCollection and overlays copies of them onto elements of the current ArrayList, starting at TargetIndex.
 
Just like CopyTo, SetRange performs a shallow copy. When the SourceCollection holds reference (non primitive) data types, the target array element values still refer to the same objects that current array elements refer to. This is what is referred to as a shallow copy. A deep copy would create new versions of the referred objects.
Microsoft MSDN Links
System.Collections
System.Collections.ArrayList
 Author links

 Buy Website Traffic at
 Buywebsitetrafficexperts.com

 Buy Proxies at
 Buyproxies.io
 
 
 
Overlaying a 4 element ArrayList onto a 10 element ArrayList
program Project1;
{$APPTYPE CONSOLE}

uses
  System.Collections;

var
  Source, Target : System.Collections.ArrayList;
  i              : Integer;

begin
  // Create our array list objects
  Source := ArrayList.Create;
  Target := ArrayList.Create;

  // Fill them
  Source.Add('Source.Zero');
  Source.Add('Source.One');
  Source.Add('Source.Two');
  Source.Add('Source.Three');

  Target.Add('Target.Zero');
  Target.Add('Target.One');
  Target.Add('Target.Two');
  Target.Add('Target.Three');
  Target.Add('Target.Four');
  Target.Add('Target.Five');
  Target.Add('Target.Six');
  Target.Add('Target.Seven');
  Target.Add('Target.Eight');
  Target.Add('Target.Nine');

  // Copy the middle 4 elements to the target array
  Console.WriteLine('Copying the Source to the middle of the target array :');
  Console.WriteLine;
  Target.SetRange(3, Source);

  // Display the target array contents
  for i := 0 to Target.Count-1 do
    Console.WriteLine(Target.Item[i].ToString);

  Console.Readline;
end.
   Copying the Source to the middle of the target array :
  
   Target.Zero
   Target.One
   Target.Two
   Source.Zero
   Source.One
   Source.Two
   Source.Three
   Target.Seven
   Target.Eight
   Target.Nine
Illustrating the shallowness of the SetRange element copy
program Project1;
{$APPTYPE CONSOLE}

uses
  System.Collections;

type
  MyClass = Class
    private
      mcName : String;
    published
      Property Name : String
          read mcName
         write mcName;
      Constructor Create(Name : String); overload;
      Function    ToString : String;     overload;
  end;

// MyClass methods
constructor MyClass.Create(Name : String);
begin
  inherited Create;
  mcName := Name;
end;

function MyClass.ToString : String;
begin
  Result := Name;
end;

// Main code

var
  Source, Target : System.Collections.ArrayList;
  MyObject       : MyClass;
  i              : Integer;

begin
  // Create our array list objects
  Source := ArrayList.Create;
  Target := ArrayList.Create;

  // Fill them
  Source.Add(MyClass.Create('Source.Zero'));
  Source.Add(MyClass.Create('Source.One'));
  Source.Add(MyClass.Create('Source.Two'));
  Source.Add(MyClass.Create('Source.Three'));

  Target.Add(MyClass.Create('Target.Zero'));
  Target.Add(MyClass.Create('Target.One'));
  Target.Add(MyClass.Create('Target.Two'));
  Target.Add(MyClass.Create('Target.Three'));
  Target.Add(MyClass.Create('Target.Four'));
  Target.Add(MyClass.Create('Target.Five'));
  Target.Add(MyClass.Create('Target.Six'));
  Target.Add(MyClass.Create('Target.Seven'));
  Target.Add(MyClass.Create('Target.Eight'));
  Target.Add(MyClass.Create('Target.Nine'));

  // Copy the middle 4 elements to the target array
  Console.WriteLine('Copying the Source to the middle of the target array :');
  Console.WriteLine;
  Target.SetRange(3, Source);

  // Display the target array contents
  for i := 0 to Target.Count-1 do
  begin
    MyObject := MyClass(Target.Item[i]);
    Console.WriteLine(MyObject.ToString);
  end;

  // Update element 1 of the source array
  Console.WriteLine;
  Console.WriteLine('Updating element 2 of the source only');
  Console.WriteLine;

  MyObject := MyClass(Source[2]);
  MyObject.Name := 'Source.Two modified';

  // Display the target array contents
  for i := 0 to Target.Count-1 do
  begin
    MyObject := MyClass(Target.Item[i]);
    Console.WriteLine(MyObject.ToString);
  end;

  Console.Readline;
end.
   Copying the Source to the middle of the target array :
  
   Target.Zero
   Target.One
   Target.Two
   Source.Zero
   Source.One
   Source.Two
   Source.Three
   Target.Seven
   Target.Eight
   Target.Nine
  
   Updating element 2 of the source only
  
   Target.Zero
   Target.One
   Target.Two
   Source.Zero
   Source.One
   Source.Two modified
   Source.Three
   Target.Seven
   Target.Eight
   Target.Nine
 
 
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Contact the author