Returns a specific member of a Collection object either by position or by key.
ReadOnly Public Default Overloads Property Item( _ ByVal Index As { Integer | Object } _ ) As Object
Exception type | Error number | Condition |
---|---|---|
9 | Index doesn't match an existing member of the collection. |
If Index doesn't match any existing member of the collection, an error occurs.
The Item property is the default property for a collection. Therefore, the following lines of code are equivalent:
Print MyCollection(1) Print MyCollection.Item(1)
This example uses the Item property to retrieve a reference to an object in a collection. Given that birthdays
is a Collection object, the following code retrieve a reference to the objects representing Bill's birthday from the collection, using the key "Bill" as the Index arguments.
Dim birthdays As New Collection() Dim aBirthday As DateTime birthdays.Add(New DateTime(2001, 1, 12), "Bill") aBirthday = birthdays("Bill") MsgBox(aBirthday.ToString()) aBirthday = birthdays.Item(
"Bill")
MsgBox(aBirthday.ToString())
Note that the first call explicitly specifies the Item property, but the second does not. Both calls work because the Item property is the default for a Collection object. The reference, assigned to aBirthday
, can be used to access the properties and methods of the specified object.
Add Method | Count Property | Remove Method |