Visual Basic Language Reference  

Item Property

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

Parameters

Object
An object expression that evaluates to an object in the Collection object.
Index
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 Index is a String expression, it must correspond to the key value specified when the member referred to was added to the collection.

Exceptions/Errors

Exception type Error number Condition
IndexOutOfRangeException 9 Index doesn't match an existing member of the collection.

Remarks

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)

Example

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.

See Also

Add Method | Count Property | Remove Method | IndexOutOfRangeException

Applies To

Collection Object