Item Method
Applies To
Collection object.
Description
Returns a specific member of a Collection object either by position or by key.
Syntax
object.Item(index)
The Item method syntax has the following object qualifier and part
Part | Description |
|
object | Required. An object expression that evaluates to an object in the Applies To list. |
index | Required. An expression that specifies the position of a member of the collection. If a numeric expression, index must be a number from 1 to the value of the collection's Count property. If a string expression, index must correspond to the key argument specified when the member referred to was added to the collection. |
Remarks
If the value provided as index doesn't match any existing member of the collection, an error occurs.
The Item method is the default method for a collection. Therefore, the following lines of code are equivalent:
Print MyCollection(1)
Print MyCollection.Item(1)
See Also
Add method, Caption property ("Extensibility Object Model Language Reference"), Count property ("Extensibility Object Model Language Reference"), Name property ("Extensibility Object Model Language Reference"), Remove method.
Example
This example uses the Item method to retrieve a reference to an object in a collection. Assuming Birthdays is a Collection object, the following code retrieves from the collection references to the objects representing Bill Smith's birthday and Adam Smith's birthday, using the keys "SmithBill" and "SmithAdam" as the index arguments. Note that the first call explicitly specifies the Item method, but the second does not. Both calls work because the Item method is the default for a Collection object. The references, assigned to SmithBillBD and SmithAdamBD using Set, can be used to access the properties and methods of the specified objects. To run this code, create the collection and populate it with at least the two referenced members.
Dim SmithBillBD As Object
Dim SmithAdamBD As Object
Dim Birthdays
Set SmithBillBD = Birthdays.Item("SmithBill")
Set SmithAdamBD = Birthdays("SmithAdam")
Example (Extensibility Object Model)
The following example contains two ways to display a specific member of the CodePanes collection, one using the Item method.
Application.VBE.CodePanes.Item(2).Show
Application.VBE.CodePanes(2).Show