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
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 retrieves 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.
Requirements
Namespace: Microsoft.VisualBasic
Module: Collection
Assembly: Microsoft Visual Basic .NET Runtime (in Microsoft.VisualBasic.dll)
See Also
Add Method | Count Property | Remove Method | IndexOutOfRangeException Class
Applies To
Collection Object