3.6 The Collection ObjectVB.NET implements a special object called the Collection object that acts as a container for objects of all types. In fact, Collection objects can hold other objects, as well as nonobject data. In some ways, the Collection object is an object-oriented version of the Visual Basic array. It supports the following four methods :
For example, the following code defines a collection object named colStates to hold information about U.S. states and then adds two members to it, using the state's two-letter abbreviation as a key: Dim colStates As New Collection colStates.Add("New York", "NY") colStates.Add("Michigan", "MI") Like members of an array, the members of a collection can be iterated using the For Each...Next construct. Also like arrays, collection members are accessible by their index value, although the lower bound of a collection object's index is always 1. Arrays and collections each have advantages and disadvantages. Some of the advantages of collections over arrays are:
Note that when deleting collection members by index, it is important to iterate though the indexes in reverse order because member deletion changes the indexes of other members. |