Home  |  Delphi .net Home  |  System.Array  |  GetEnumerator Method
  
GetEnumerator  
Method  
Gets an enumerator to allow reading the elements of the current array
Array Class
System NameSpace
CF1.  Function GetEnumerator : IEnumerator;
CF : Methods with this mark are Compact Framework Compatible
Description
Returns an IEnumerator object that allows the complete contents of a single of multidimensional array to be read.
References
IEnumerator
Microsoft MSDN Links
System
System.Array
 Author links

 Buy Website Traffic at
 Buywebsitetrafficexperts.com

 Buy Proxies at
 Buyproxies.io
 
 
 
Displaying the elements of a single dimension array
program Project1;
{$APPTYPE CONSOLE}

uses
  System.Collections;

var
  myArray    : System.Array;
  enumerator : System.Collections.IEnumerator;

begin
  // Create a 4 element single dimension array of strings
  myArray := System.Array.CreateInstance(TypeOf(String), 4);

  // Fill the array with values
  myArray.SetValue('Hello',  0);
  myArray.SetValue('from',   1);
  myArray.SetValue('Delphi', 2);
  myArray.SetValue('Basics', 3);

  // Display the array contents using an enumerator
  enumerator := myArray.GetEnumerator;

  // We must move to the start before displaying the first value
  while Enumerator.MoveNext do
    Console.WriteLine(Enumerator.Current.ToString);

  Console.ReadLine;
end.
   Hello
   from
   Delphi
   Basics
Displaying the elements of a two dimensional array
program Project1;
{$APPTYPE CONSOLE}

uses
  System.Collections;

var
  myArray    : System.Array;       // .Net array
  lengths    : Array of Integer;   // Native Delphi dynamic array
  value      : Integer;
  i0, i1     : Integer;
  Enumerator : System.Collections.IEnumerator;

begin
  // First, we define the array that holds the lengths of each
  // dimension of our multi-dimensional array
  SetLength(lengths, 2);  // Dynamic arrays are always 0 based

  lengths[0] := 2;
  lengths[1] := 5;

  // Create a multi dimensional array of integers
  myArray := System.Array.CreateInstance(TypeOf(Integer), lengths);

  // Fill the array with values
  for i0 := 0 to myArray.GetUpperBound(0) do
    for i1 := 0 to myArray.GetUpperBound(1) do
      begin
        // Create an element value according to its array position
        value := (i0 + 1) * (i1 + 1);
        myArray.SetValue(TObject(value), i0, i1);
      end;

  // Display the array contents
  for i0 := 0 to myArray.GetUpperBound(0) do
    for i1 := 0 to myArray.GetUpperBound(1) do
      begin
        value := Integer(myArray.GetValue(i0, i1));
        Console.WriteLine('myArray[ ' + i0.ToString + ', '   +
                                        i1.ToString + '] = ' +
                                        value.ToString);
      end;

  Console.WriteLine;

  // Display the array contents using an enumerator
  Enumerator := myArray.GetEnumerator;

  while Enumerator.MoveNext do
    Console.WriteLine(Enumerator.Current.ToString);

  Console.ReadLine;
end.
   myArray[ 0, 0] = 1
   myArray[ 0, 1] = 2
   myArray[ 0, 2] = 3
   myArray[ 0, 3] = 4
   myArray[ 0, 4] = 5
   myArray[ 1, 0] = 2
   myArray[ 1, 1] = 4
   myArray[ 1, 2] = 6
   myArray[ 1, 3] = 8
   myArray[ 1, 4] = 10
  
   1
   2
   3
   4
   5
   2
   4
   6
   8
   10
 
 
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Contact the author