Pass Collections as Parameters
Suppose you want to call a function that fills an array and the array is a member of an object. Other than a kidney stone, there isn't anything harder to pass than a member array. Since arrays must be passed by reference using the @ operator, and you can't use @ with a member array, you have to pass a local array and then ACOPY() the local array into the member array. However, to avoid an error, you must DIMENSION the member array properly first. I have a lot of code similar to this in various applications:
lnRows = alen(laItems, 1)
lnCols = alen(laItems, 2)
dimension This.aItems(lnRows, lnCols)
Using a member collection rather than a member array (and assuming SomeFunction
can work with a collection), this becomes as simple as:
Use Collections of Collections
This.oItems = createobject('Collection')
The items in a collection can be anything, including other collections. In addition to acting like multi-dimensional arrays, collections of collections allow you to address objects at any level of detail using simple syntax.
Suppose you want to work with metadata. Wouldn't it be nice to retrieve the data type for a field using code like the following?
If Tables is a collection of Table objects, and a Table object has a Fields collection of Field objects, and a Field object has a DataType
property, this is easily done.
shows an example of this. The Init method of the Tables class populates the collections of tables and fields by reading metadata from a table called CoreMeta.dbf
. This table has columns of information about tables and fields, including cRecType ("T")
for a table and "F"
for a field), cObjectNam
(the name of the table or field), and cType
(the data type of a field).
To determine the number of fields in the Customer table, use the following:
You can get the descriptive name for the Orders.Order_Date
field with this:
The new VFP 8 Collection base class makes it easy to create and work with collections of items. While arrays still have their place, I predict collections will replace them in the majority of uses in applications as VFP developers become more familiar with them.