Home  |  Delphi .net Home  |  System.Array  |  GetLength Method
GetLength  
Method  
Gets the length of a dimension of the current array
Array Class
System NameSpace
CF1.  Function GetLength ( Dimension : Integer; ) : Integer;
CF : Methods with this mark are Compact Framework Compatible
Description
Returns the size (number of elements) of the specified Dimension of the current array.
Notes
Unlike native Delphi arrays, multidimensional .Net arrays are self contained.

A native Delphi multidimensional array is literally an array of arrays - each base array element contains an independant array that is simply referenced by that element.
Microsoft MSDN Links
System
System.Array
 
 
Getting the dimension lengths for a multi dimensional array
program Project1;
{$APPTYPE CONSOLE}

var
  myArray : System.Array;       // .Net array
  lengths : Array of Integer;   // Native Delphi dynamic array
  value   : Integer;
  dim     : Integer;

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

  lengths[0] := 2;
  lengths[1] := 3;
  lengths[2] := 4;

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

  // Display the array dimension sizes (lengths)
  for dim := 0 to myArray.Rank-1 do   // Rank = number of dimensions
    Console.WriteLine('Dimension {0} size = {1}',
                      dim.ToString, myArray.GetLength(dim).ToString);

  Console.ReadLine;
end.
Show full unit code
  Dimension 0 size = 2
  Dimension 1 size = 3
  Dimension 2 size = 4
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author