Home  |  Delphi .net Home  |  System.Array  |  Clear Method
Clear  
Method  
Sets a range of elements in an Array to zero, false, or null as appropriate
Array Class
System NameSpace
CF1.  Procedure Clear ( TheArray:Array of ObjectTheArray : Array of Object; Start : Integer; Count : Integer; ) ; Static;
CF : Methods with this mark are Compact Framework Compatible
Description
Clears up to the values of up to Count elements from Start of TheArray.
 
The clearing process depends on data type :
 
Strings are set to ''
 
Numbers are set to 0
 
Booleans are set to false
 
References are set to null.
Notes
The array element values are updated - the elements persist.
Microsoft MSDN Links
System
System.Array
 
 
A simple example
program Project1;
{$APPTYPE CONSOLE}

var
  strArray  : System.Array;
  i         : Integer;

begin
  // Create a 3 element single dimension array of Strings
  strArray := System.Array.CreateInstance(TypeOf(String), 3);

  // Fill the array
  strArray.SetValue('ABC', 0);
  strArray.SetValue('DEF', 1);
  strArray.SetValue('GHI', 2);

  // Display the contents
  for i := 0 to strArray.Length-1 do
    Console.WriteLine('Element {0} = {1}', i.ToString, strArray.GetValue(i));

  // Clear the array
  Console.WriteLine('Clearing the array');
  System.Array.Clear(strArray, 0, strArray.Length);

  // Display the contents
  for i := 0 to strArray.Length-1 do
    Console.WriteLine('Element {0} = {1}', i.ToString, strArray.GetValue(i));

  Console.ReadLine;
end.
Show full unit code
  Element 0 = ABC
  Element 1 = DEF
  Element 2 = GHI
  Clearing the array
  Element 0 =
  Element 1 =
  Element 2 =
Clearing just a subset of elements
program Project1;
{$APPTYPE CONSOLE}

var
  intArray  : System.Array;
  i         : Integer;

begin
  // Create a single dimension array of Integers
  intArray := System.Array.CreateInstance(TypeOf(Integer), 5);

  // Fill the array
  for i := 0 to intArray.Length-1 do
    intArray.SetValue(TObject((i+2)*5), i);

  // Display the contents
  for i := 0 to intArray.Length-1 do
    Console.WriteLine('Element {0} = {1}', i.ToString, intArray.GetValue(i).ToString);

  // Clear the array
  Console.WriteLine('Clearing middle 3 array elements');
  System.Array.Clear(intArray, 1, 3);

  // Display the contents
  for i := 0 to intArray.Length-1 do
    Console.WriteLine('Element {0} = {1}', i.ToString, intArray.GetValue(i).ToString);

  Console.ReadLine;
end.
Show full unit code
  Element 0 = 10
  Element 1 = 15
  Element 2 = 20
  Element 3 = 25
  Element 4 = 30
  Clearing middle 3 array elements
  Element 0 = 10
  Element 1 = 0
  Element 2 = 0
  Element 3 = 0
  Element 4 = 30
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author