RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
Variants.VarArrayPut Function

Sets the value of a single cell in a multi-dimensional System::Variant array.

Pascal
procedure VarArrayPut(var A: Variant; const Value: Variant; const Indices: array of Integer);
C++
VarArrayPut(Variant A, const Variant Value, const array of Integer Indices);

Use VarArrayPut to set the value of a single cell in a multi-dimensional System::Variant array. 

A is the System::Variant array that contains the cell to set. 

Value is the value to assign to the specified cell. 

Indices is an array of index values, one for each dimension of the array.

Note: Indices_Size is the index of the last value in Indices (one less than the number of dimensions in the array).
If the values specified by Indices are out of bounds, VarArrayPut raises an EVariantBadIndexError.  

C++ Examples: 

 

/*
This example demonstrates the use of variant arrays. In this
example a variant array is created, modified, populated and then freed.
*/
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    Variant array;
    int bounds[2] = {0, 9};

    /*
    Create a variant array of 10 elements, starting at 0
    and ending at 9. The array contains elements of type integer
    */
    array = VarArrayCreate(bounds, 1, varInteger);

    // Increase the length of the variant array
    VarArrayRedim(array, 49);

    MessageDlg(String("Variant array has ") + IntToStr(VarArrayDimCount(array)) +
        " dimensions", mtInformation, TMsgDlgButtons() << mbOK, 0);

    // Traverse the array from lower to higher bounds
    for (int i = VarArrayLowBound(array, 1); i <= VarArrayHighBound(array, 1); i++)
    {
        // Put the element I at index I
        VarArrayPut(array, i, &i, 0);
    }

    // Now read the elements of the array
    for (int i = VarArrayLowBound(array, 1); i <= VarArrayHighBound(array, 1); i++)
    {
        // Put the element I at index I
        if (VarArrayGet(array, &i, 0) != i)
            MessageDlg("Error! Invalid element found at current position!",
                mtError, TMsgDlgButtons() << mbOK, 0);
    }

    // Clear the variant array
    VarClear(array);
}

 

Delphi Examples: 

{
This example demonstrates the use of variant arrays. In this
example a variant array is created, modified, populated and then freed.
}
procedure TForm1.Button1Click(Sender: TObject);
var
  Arr: Variant;
  I: Integer;
begin
  { Create a variant array of 10 elements, starting at 0
    and ending at 9. The array contains elements of type integer }
  Arr := VarArrayCreate([0, 9], varInteger);

  { Increase the length of the variant array }
  VarArrayRedim(Arr, 49);

  MessageDlg('Variant array has ' + IntToStr(VarArrayDimCount(Arr)) + ' dimensions',
      mtInformation, [mbOK], 0);

  { Traverse the array from lower to higher bounds }
  for I := VarArrayLowBound(Arr, 1) to VarArrayHighBound(Arr, 1) do
  begin
    { Put the element I at index I}
    VarArrayPut(Arr, I, [I]);
  end;

  { Now read the elements of the array }
  for I := VarArrayLowBound(Arr, 1) to VarArrayHighBound(Arr, 1) do
  begin
    { Put the element I at index I}
    if VarArrayGet(Arr, [I]) <> I then
      MessageDlg('Error! Invalid element found at current position!', mtError, [mbOK], 0);
  end;

  { Clear the variant array }
  VarClear(Arr);
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!