Download VB5 User's Guide
Transcript
Tip For better performance, use For Each to iterate over the items in a Collection object. For Each is significantly faster than iterating with the index. This is not true of all collection implementations it’s dependent on the way the collection stores data internally. Adding Items to a Collection Use the Add method to add an item to a collection. The syntax is: Sub Add (item As Variant [, key As Variant] [, before As Variant] [, after As Variant] ) For example, to add a work order object to a collection of work orders using the work order’s ID property as the key, you can write: colWorkOrders.Add woNew, woNew.ID This assumes that the ID property is a String. If the property is a number (for example, a Long), use the CStr function to convert it to the String value required for keys: colWorkOrders.Add woNew, CStr(woNew.ID) The Add method supports named arguments. To add an item as the third element, you can write: colWorkOrders.Add woNew, woNew.ID, after:=2 You can use the before and after named arguments to maintain an ordered collection of objects. For example, before:=1 inserts an item at the beginning of the collection, because Collection objects are one-based. Deleting Items from a Collection Use the Remove method to delete an item from a collection. The syntax is: object.Remove index The index argument can either be the position of the item you want to delete, or the item’s key. If the key of the third element in a collection is "W017493," you can use either of these two statements to delete it: colWorkOrders.Remove 3 or colWorkOrders.Remove "W017493" Retrieving Items from a Collection Use the Item method to retrieve specific items from a collection. The syntax is: [Set] variable = object.Item(index) As with the Remove method, the index can be either the position in the collection, or the item’s key. Using the same example as for the Remove method, either of these statements will retrieve the third element in the collection: Set woCurrent = colWorkOrders.Item(3) or