Collection.Item Property (String)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Returns a specific element of a Collection object either by position or by key. Read-only.

Namespace:  Microsoft.VisualBasic
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)

Syntax

'Declaration
Public ReadOnly Property Item ( _
    Key As String _
) As Object
public Object this[
    string Key
] { get; }

Parameters

  • Key
    Type: System.String
    A unique String expression that specifies a key string that can be used, instead of a positional index, to access an element of the collection. Key must correspond to the Key argument specified when the element was added to the collection.

Property Value

Type: System.Object
Returns a specific element of a Collection object either by position or by key. Read-only.

Remarks

If Index is of type Object, the Item property attempts to treat it as a String, Char, Char array, or integer value. If Item cannot convert Index to String or Integer, it throws an ArgumentException exception.

The Item property is the default property for a collection. Therefore, the following lines of code are equivalent.

MsgBox(CStr(customers.Item(1)))
MsgBox(CStr(customers(1)))

Examples

The following example uses the Item property to retrieve a reference to an object in a collection. It creates birthdays as a Collection object and then retrieves the object representing Bill's birthday, using the key "Bill" as the Index argument.

Dim Msg As String

Dim birthdays As New Collection()
birthdays.Add(New DateTime(2001, 1, 12), "Bill")
birthdays.Add(New DateTime(2001, 1, 13), "Joe")
birthdays.Add(New DateTime(2001, 1, 14), "Mike")
birthdays.Add(New DateTime(2001, 1, 15), "Pete")


...


Dim aBirthday As DateTime
aBirthday = birthdays.Item("Bill")
Msg = CStr(aBirthday)
aBirthday = birthdays("Bill")
Msg = CStr(aBirthday)

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 property for a Collection object.

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.