O'Reilly logo

Python Programming On Win32 by Mark Hammond, Andy Robinson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required