แก้ไข

แชร์ผ่าน


Collection.Item[] Property

Definition

Gets a specific element of a Collection object either by position or by key.

Overloads

Item[Int32]

Gets a specific element of a Collection object either by position or by key.

Item[Object]

Gets a specific element of a Collection object either by position or by key.

Item[String]

Gets a specific element of a Collection object either by position or by key.

Item[Int32]

Source:
Collection.vb
Source:
Collection.vb
Source:
Collection.vb

Gets a specific element of a Collection object either by position or by key.

public:
 property System::Object ^ default[int] { System::Object ^ get(int Index); };
public object? this[int Index] { get; }
public object this[int Index] { get; }
member this.Item(int) : obj
Default Public ReadOnly Property Item(Index As Integer) As Object

Parameters

Index
Int32

A numeric expression that specifies the position of an element of the collection. Index must be a number from 1 through the value of the collection's Count property. Or (B) An Object expression that specifies the position or key string of an element of the collection.

Property Value

A specific element of a Collection object either by position or by key.

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 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")
MsgBox(CStr(aBirthday))
aBirthday = birthdays("Bill")
MsgBox(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.

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)))  

Applies to

Item[Object]

Source:
Collection.vb
Source:
Collection.vb
Source:
Collection.vb

Gets a specific element of a Collection object either by position or by key.

public:
 property System::Object ^ default[System::Object ^] { System::Object ^ get(System::Object ^ Index); };
public object? this[object Index] { get; }
public object this[object Index] { get; }
member this.Item(obj) : obj
Default Public ReadOnly Property Item(Index As Object) As Object

Parameters

Index
Object

A numeric expression that specifies the position of an element of the collection. Index must be a number from 1 through the value of the collection's Count property. Or (B) An Object expression that specifies the position or key string of an element of the collection.

Property Value

A specific element of a Collection object either by position or by key.

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 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")
MsgBox(CStr(aBirthday))
aBirthday = birthdays("Bill")
MsgBox(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.

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)))  

Applies to

Item[String]

Source:
Collection.vb
Source:
Collection.vb
Source:
Collection.vb

Gets a specific element of a Collection object either by position or by key.

public:
 property System::Object ^ default[System::String ^] { System::Object ^ get(System::String ^ Key); };
public object? this[string Key] { get; }
public object this[string Key] { get; }
member this.Item(string) : obj
Default Public ReadOnly Property Item(Key As String) As Object

Parameters

Key
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

A specific element of a Collection object either by position or by key.

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 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")
MsgBox(CStr(aBirthday))
aBirthday = birthdays("Bill")
MsgBox(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.

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)))  

Applies to