Browse DevX
Sign up for e-mail newsletters from DevX


Collections Are Cool! : Page 2

Visual FoxPro 8.0 includes a new Collection base class. In this article, Doug describes some uses for collections and why they are superior to arrays.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

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:

   dimension laItems[1]
   lnRows = alen(laItems, 1)
   lnCols = alen(laItems, 2)
   dimension This.aItems(lnRows, lnCols)
   acopy(laItems, This.aItems)
Using a member collection rather than a member array (and assuming SomeFunction can work with a collection), this becomes as simple as:

   This.oItems = createobject('Collection')
Use Collections of Collections
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.

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

Doug Hennig is a partner with Stonefield Systems Group Inc. He is the author of the award-winning Stonefield Database Toolkit (SDT), the award-winning Stonefield Query, and the CursorAdapter and DataEnvironment builders that ship with Microsoft Visual FoxPro 8. Doug is co-author of "What's New in Visual FoxPro 8.0," "The Hacker's Guide to Visual FoxPro 7.0," and "What's New in Visual FoxPro 7.0". He was the technical editor of "The Hacker's Guide to Visual FoxPro 6.0" and "The Fundamentals." All these books are from Hentzenwerke Publishing, (www.hentzenwerke.com). Doug writes the monthly "Reusable Tools" column in FoxTalk. He has spoken at every Microsoft FoxPro Developers Conference (DevCon) since 1997 and at user groups and developer conferences all over North America. He is a Microsoft Most Valuable Professional (MVP) and Certified Professional (MCP). See www.stonefield.com or www.stonefieldquery.com for more information about Stonefield products or email Doug at dhennig@stonefield.com
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date