Export (0) Print
Expand All

Adding, Deleting, and Retrieving Items from a Collection

The Collection class contains built-in functionality to allow you to add, delete and retrieve items. The use of this functionality is demonstrated below.

Adding Items to a Collection

Use the Add method to add an item to a collection. The syntax is:

object.Add(Item, Key)

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:

workordersCollection.Add(woNew, woNew.ID)

This assumes that the ID property is a string. If the property is a number (for example, a Long integer), use its .ToString function to convert it to the String value required for keys:

workordersCollection.Add(woNew, woNew.ID.ToString)

The use of a key is optional. If you do not want to associate a key with the object in your collection, you may add it without a key, as shown below:

workordersCollection.Add(woNew)

You can use the before and after arguments to maintain an ordered collection of objects. The member to be added is placed in the collection before or after the member identified by the before or after argument, respectively. For example, setting before equal to 1 inserts an item at the beginning of the collection, because Collection objects are one-based, as in the example that follows:

workordersCollection.Add(woNew, woNew.ID, 1)

Similarly, the after argument adds an item after the specified index. The following example adds an item as the third element:

workordersCollection.Add(woNew, woNew.ID,,2)

You can specify a value for either before or after, but not both.

Deleting Items from a Collection

Use the Remove method to delete an item from a collection. The syntax is:

object.Remove(index | key)

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:

workordersCollection.Remove(3)
workordersCollection.Remove("W017493")

Retrieving Items from a Collection

Use the Item property to retrieve specific items from a collection. The syntax is:

variable = object.Item(index)

As with the Remove method, the index can be either the item's position in the collection or the item's key. Using the same example as for the Remove method, either of these statements retrieves the third element in the collection:

woCurrent = workordersCollection.Item(3)
woCurrent = workordersCollection.Item("W017493")
Note   If you use whole numbers as keys, you must use the .ToString function to convert them to strings before passing them to the Add, Remove, or Item property. A Collection object always assumes that a whole number is an index.

Item Is the Default Property

The Item property is the default property for a Collection object, so you can omit it when you access an item in a collection. Thus, the previous code examples could also be written as follows:

woCurrent = workordersCollection(3) 
woCurrent = workordersCollection("W017493")

You can also use the ! member access qualifier to access a member of the collection by its key without qualifying the key with quotation marks or parentheses. Thus the previous example can also be written as follows:

woCurrent = workordersCollection!W017493
Note   Collection objects update their numeric index numbers automatically as you add and delete elements; the numeric index of a given element thus often changes. Therefore, do not save a numeric index value and expect it to retrieve the same element later in your program. Use keys for this purpose.

See Also

Properties and Methods of the Collection Class | The Visual Basic .NET Collection Class | Collections as an Alternative to Arrays | Collections in Visual Basic .NET | Add Method | Remove Method | Item Property

Show:
© 2014 Microsoft