Unpacking Variant Arrays
Delphi’s handling of variant arrays
is somewhat closer to that of C or C++ than Visual Basic. It provides
three functions to find the number of dimensions in a variant array
and the high and low bounds of a given dimension. The first of these,
VarArrayDimCount, is extremely useful and
something VB lacks; the easiest way to find this from VB is to ask
for the bounds of higher and higher dimensions until an error occurs.
VarArrayHighBound(array,
dimension) and
VarArrayHighBound(array,
dimension) are the equivalents of
UBound() and LBound(). Arrays
returned from Python always have a lower bound of zero.
If you want to iterate over a 1D array, you can’t use a
for
each loop as in VB; instead
you need to find the upper bound of the array. Here’s the code
to update a list box of accounts:
procedure TfrmMain.UpdateAccountList;
var AccountList: Variant;
i: integer;
begin
lstAllAccounts.Items.Clear;
AccountList := BookServer.GetAccountList;
for i := 0 to VarArrayHighBound(AccountList, 1) do
lstAllAccounts.Items.Add(AccountList[i]);
end;The array has one dimension, so ask for the upper bound of dimension 1.
The expression AccountList[i] returns a
Variant, which Delphi coerces to a string when
adding to the list box.
Delphi also offers more functions for constructing
Variant arrays of given dimensions, which can
efficiently pass data to Python.
Callbacks, or the lack thereof
Unfortunately this isn’t so easy as with Visual Basic. In VB, every user interface object and class ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access