VariantArrays (Delphi)
Description
This example demonstrates the use of variant arrays. In this example, a variant array is created, modified, populated, and then freed.
Code
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;
Uses
- System.Variants.VarArrayCreate ( fr | de | ja )
- System.Variants.VarArrayDimCount ( fr | de | ja )
- System.Variants.VarArrayLowBound ( fr | de | ja )
- System.Variants.VarArrayHighBound ( fr | de | ja )
- System.Variants.VarArrayPut ( fr | de | ja )
- System.Variants.VarArrayGet ( fr | de | ja )
- System.VarArrayRedim ( fr | de | ja )