Home  |  Delphi .net Home  |  System.Array  |  CreateInstance Method
  
CreateInstance  
Method  
Creates a single or multi dimensional array
Array Class
System NameSpace
CF1.  Function CreateInstance ( ArrayType : Type; Length : Integer ) : System.Array; Static;
NotCF2.  Function CreateInstance ( ArrayType : Type; Dim0Length : Integer; Dim1Length : Integer ) : System.Array; Static;
NotCF3.  Function CreateInstance ( ArrayType : Type; Dim0Length : Integer; Dim1Length : Integer; Dim2Length : Integer ) : System.Array; Static;
CF4.  Function CreateInstance ( ArrayType : Type; Lengths : Array of Integer ) : System.Array; Static;
NotCF5.  Function CreateInstance ( ArrayType : Type; Lengths : Array of Int64 ) : System.Array; Static;
NotCF6.  Function CreateInstance ( ArrayType : Type; Lengths : Array of Integer; LowerBounds : Array of Integer ) : System.Array; Static;
CF : Methods with this mark are Compact Framework Compatible
Description
Creates single or multidimensional arrays.
 
Do not get confused with the native Delphi Array type. The latter is retained, allowing for primitive static (size fixed in the declaration) and dynamic arrays. On this site, .Net arrays will be identified as System.Array types.
 
Note that the second syntax uses one of these native array primitives to hold the sizes of each dimension of the array.
 
.Net arrays are 0 based unless you specify (as in the last syntax above) lower bounds for each dimension.
Notes
Unlike native Delphi arrays, multidimensional .Net arrays are self contained - a native multidimensional array is literally an array of arrays - each base array element contains an independant array that is simply referenced by that element.

Static methods are not methods of an object - they are simply class functions or procedures available at any time.
Microsoft MSDN Links
System
System.Array
 Author links

 Buy Website Traffic at
 Buywebsitetrafficexperts.com

 Buy Proxies at
 Buyproxies.io
 
 
 
Creating a single dimension array
program Project1;
{$APPTYPE CONSOLE}

var
  myArray : System.Array;
  i       : Integer;

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
  for i := 0 to myArray.Length-1 do  // Arrays are always 0 based
    Console.WriteLine('myArray[{0}] = {1}',
                       i.ToString, myArray.GetValue(i));

  Console.ReadLine;
end.
   myArray[0] = Hello
   myArray[1] = from
   myArray[2] = Delphi
   myArray[3] = Basics
Creating a multi dimensional array
program Project1;
{$APPTYPE CONSOLE}

var
  myArray     : System.Array;       // .Net array
  lengths     : Array of Integer;   // Native Delphi dynamic array
  value       : Integer;
  i0, i1, i2  : 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);

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

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

  Console.ReadLine;
end.
   myArray[ 0, 0, 0] = 0
   myArray[ 0, 0, 1] = 1
   myArray[ 0, 0, 2] = 2
   myArray[ 0, 0, 3] = 3
   myArray[ 0, 1, 0] = 1
   myArray[ 0, 1, 1] = 2
   myArray[ 0, 1, 2] = 3
   myArray[ 0, 1, 3] = 4
   myArray[ 0, 2, 0] = 2
   myArray[ 0, 2, 1] = 3
   myArray[ 0, 2, 2] = 4
   myArray[ 0, 2, 3] = 5
   myArray[ 1, 0, 0] = 1
   myArray[ 1, 0, 1] = 2
   myArray[ 1, 0, 2] = 3
   myArray[ 1, 0, 3] = 4
   myArray[ 1, 1, 0] = 2
   myArray[ 1, 1, 1] = 3
   myArray[ 1, 1, 2] = 4
   myArray[ 1, 1, 3] = 5
   myArray[ 1, 2, 0] = 3
   myArray[ 1, 2, 1] = 4
   myArray[ 1, 2, 2] = 5
   myArray[ 1, 2, 3] = 6
Creating a multidimensional array with non-zero lower bounds
program Project1;
{$APPTYPE CONSOLE}

var
  myArray     : System.Array;       // .Net array
  lengths     : Array of Integer;   // Native Delphi dynamic array
  lowerBounds : Array of Integer;   // Native Delphi dynamic array
  value       : Integer;
  i0, i1, i2  : 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;

  // Then our array that holds the lower bounds of each dimension
  SetLength(lowerBounds, 3);

  lowerBounds[0] := 10;
  lowerBounds[1] := 20;
  lowerBounds[2] := 30;

  // Create a multi dimensional array of strings with our given
  // dimension sizes and lower bounds
  myArray := System.Array.CreateInstance(TypeOf(Integer),
                                         lengths, lowerBounds);

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

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

  Console.ReadLine;
end.
   myArray[ 10, 20, 30] = 60
   myArray[ 10, 20, 31] = 61
   myArray[ 10, 20, 32] = 62
   myArray[ 10, 20, 33] = 63
   myArray[ 10, 21, 30] = 61
   myArray[ 10, 21, 31] = 62
   myArray[ 10, 21, 32] = 63
   myArray[ 10, 21, 33] = 64
   myArray[ 10, 22, 30] = 62
   myArray[ 10, 22, 31] = 63
   myArray[ 10, 22, 32] = 64
   myArray[ 10, 22, 33] = 65
   myArray[ 11, 20, 30] = 61
   myArray[ 11, 20, 31] = 62
   myArray[ 11, 20, 32] = 63
   myArray[ 11, 20, 33] = 64
   myArray[ 11, 21, 30] = 62
   myArray[ 11, 21, 31] = 63
   myArray[ 11, 21, 32] = 64
   myArray[ 11, 21, 33] = 65
   myArray[ 11, 22, 30] = 63
   myArray[ 11, 22, 31] = 64
   myArray[ 11, 22, 32] = 65
   myArray[ 11, 22, 33] = 66
 
 
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Contact the author