Description |
Returns the value at the specified array position.
For a single dimensional array, Index provides the position.
For multidimentional arrays, Index0, Index1, Index2 or Indices specify the dimension positions
|
|
Microsoft MSDN Links |
System
System.Array
|
|
|
Getting values from 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.
| Show full unit code | myArray[0] = Hello
myArray[1] = from
myArray[2] = Delphi
myArray[3] = Basics
| | Getting values from a multidimensional array | program Project1;
{$APPTYPE CONSOLE}
var
myArray : System.Array; // .Net array
lengths : Array of Integer; // Native Delphi dynamic array
value : Integer;
i0, i1 : Integer;
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] := 3;
// 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 + i1;
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
// Get the array element value
value := Integer(myArray.GetValue(i0, i1));
Console.WriteLine('myArray[ ' + i0.ToString + ', ' +
i1.ToString + '] = ' +
value.ToString);
end;
Console.ReadLine;
end.
| Show full unit code | myArray[ 0, 0] = 0
myArray[ 0, 1] = 1
myArray[ 0, 2] = 2
myArray[ 1, 0] = 1
myArray[ 1, 1] = 2
myArray[ 1, 2] = 3
|
|
|
|