Collection Methods

Methods for collections are described in the following table. The Item method is required; other methods are optional.

Method name Return type Description
Add VT_DISPATCH or VT_EMPTY Adds an item to a collection. Returns VT_DISPATCH if object is created (object cannot exist outside the collection) or VT_EMPTY if no object is created (object can exist outside the collection).
Item Varies with type of collection Returns the indicated item in the collection. Required. The Item method may take one or more arguments to indicate the element within the collection to return. This method is the default member (DISPID_VALUE) for the collection object.
Remove VT_EMPTY Removes an item from a collection. Uses indexing arguments in the same way as the Item method.

All collection objects must provide at least one form of indexing through the Item method. The dispatch identifier (DISPID) of the Item method is DISPID_VALUE. Because it is the default member, it can be used in the following form:

ThirdDef = MyWords(3).Definition 

Which is equivalent to:

MyWords.Item(3).Definition

The Item method takes one or more arguments to indicate the index. Indexes can be numbers, strings, or other types. For example:

DogDef = MyWords("dog").Definition

Note Within the application's type library, the _NewEnum property has a special DISPID: DISPID_NEWENUM. The name _NewEnum should not be localized.

The Add method may take one or more arguments. For example, if MyWord is an object with the properties Letters and Definition:

Dim MyWord As New Word
Dim MyDictionary As Words
MyWord = "dog"
MyWord.Letters = "Dog"
MyWord.Definition = "My best friend."
MyDictionary.Add MyWord
MyDictionary.Remove("Dog")

For more information about creating collection objects, see Chapter 2, "Exposing ActiveX Objects."