aItems, 1) lnCols = alen(laItems, 2) dimension This.aItems(lnRows, lnCols) acopy(laItems, This.aItems) 使用成员集合要比成员数组好很多(假设SomeFunction函数可以正确的用到集合),这将变 得和下面一样的简单: This.oItems = createobject('Collection') SomeFunction(This.oItems)
使用集合的集合
集合中的条目可以是任何东西,包括其它的集合。除了有些像多维数组之外,集合的集合允许你 使用简单的语句访问任意位置中的对象。 假想你要处理变化的数据,用下面的代码来取得数据类型不是很nice吗? Tables('Products').Fields('ProductID').DataType 如果Tables是一个表对象的集合,并且表对象有一个字段对象的集合,并且字段对象有一个 DataType属性,这一切都将很容易做到。 附录1 中就是这样的一个例子。 Tables类的Init方法中通过从CoreMeta.dbf表中读取的变 化的数据,添加表和字段的集合。表中有关于表和字段的信息的列,包括cRecType 数据类型 ("T"表示表,"F" 表示字段),cObjectNam (表或字段的名称),以及cType (字段的数 据类型)。 为了确定Customer 表中字段的数目,用下面的方法: Tables('customer').Fields.Count 你还可以通过下的方法得到Orders.Order_Date字段的标题: Tables('orders').Fields('order_date').Caption
总结
VFP 8新的集合类使得创建和使用集合中的条目变得简单。当前,数组仍然占据着它们的地位, 但我估计,当VFP开发者更了解集合的时候,在大多数的应用程序中,集合将取代数组的使用。 附录1: 使用集合的集合建立变化的数据
define class Tables as Collection procedure Init local lcTable, loTable, lcField, loField use CoreMeta scan do case * If this is a table, add it to the collection. case CoreMeta.cRecType = 'T' lcTable = trim(CoreMeta.cObjectNam) loTable = createobject('Table') This.Add(loTable, lcTable)
* If this is a field, add it to the appropriate table. case CoreMeta.cRecType = 'F' lcTable = juststem(CoreMeta.cObjectNam) lcField = trim(justext(CoreMeta.cObjectNam)) loField = createobject('Field') with loField .DataType .Length .Decimals .Binary .Caption endwith This.Item(lcTable).Fields.Add(loField, lcField) endcase endscan use in CoreMeta endproc enddefine define class Table as Custom add object Fields as Collection enddefine define class Field as Custom DataType Length Decimals Binary Caption enddefine = '' = 0 = 0 = .F. = '' = CoreMeta.cType = CoreMeta.nSize = CoreMeta.nDecimals = CoreMeta.lBinary = trim(CoreMeta.cCaption)
.AllowNulls = CoreMeta.lNull
AllowNulls = .F.