Home  |  Delphi .net Home  |  System.Array  |  SetValue Method
  
SetValue  
Method  
Sets the value in an element of the current array
Array Class
System NameSpace
CF1.  Procedure SetValue ( Index : Integer ) ;
NotCF2.  Procedure SetValue ( Index0 : Integer; Index1 : Integer ) ;
NotCF3.  Procedure SetValue ( Index0 : Integer; Index1 : Integer; Index2 : Integerect ) ;
NotCF4.  Procedure SetValue ( Index : Int64 ) ;
NotCF5.  Procedure SetValue ( Index0 : Int64; Index1 : Int64 ) ;
NotCF6.  Procedure SetValue ( Index0 : Int64; Index1 : Int64; Index2 : Int64 ) ;
CF7.  Procedure SetValue ( Indices : Array of Integer ) ;
NotCF8.  Procedure SetValue ( Indices : Array of Int64 ) ;
CF : Methods with this mark are Compact Framework Compatible
Description
Sets 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
 Author links

 Buy Website Traffic at
 Buywebsitetrafficexperts.com

 Buy Proxies at
 Buyproxies.io
 
 
 
Setting values in 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
Setting values in 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.
   myArray[ 0, 0] = 0
   myArray[ 0, 1] = 1
   myArray[ 0, 2] = 2
   myArray[ 1, 0] = 1
   myArray[ 1, 1] = 2
   myArray[ 1, 2] = 3
 
 
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Contact the author