Collection.Item[String] 屬性

定義

依位置或索引鍵取得 Collection 物件的特定元素。Gets a specific element of a Collection object either by position or by key.

多載

Item[Int32]

依位置或索引鍵取得 Collection 物件的特定元素。Gets a specific element of a Collection object either by position or by key.

Item[Object]

依位置或索引鍵取得 Collection 物件的特定元素。Gets a specific element of a Collection object either by position or by key.

Item[String]

依位置或索引鍵取得 Collection 物件的特定元素。Gets a specific element of a Collection object either by position or by key.

Item[Int32]

依位置或索引鍵取得 Collection 物件的特定元素。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; }
member this.Item(int) : obj
Default Public ReadOnly Property Item(Index As Integer) As Object

參數

Index
Int32

數值運算式,其指定集合中項目的位置。A numeric expression that specifies the position of an element of the collection. Index 必須是從 1 到集合 Count 屬性值的一個數字。Index must be a number from 1 through the value of the collection's Count property. 或是 (B) Object 運算式,其指定集合中項目的位置或索引鍵字串。Or (B) An Object expression that specifies the position or key string of an element of the collection.

屬性值

依位置或索引鍵 Collection 物件的特定元素。A specific element of a Collection object either by position or by key.

範例

下列範例會使用 Item 屬性來抓取集合中物件的參考。The following example uses the Item property to retrieve a reference to an object in a collection. 它會建立 birthdays 做為 Collection 物件,然後使用索引鍵 "Bill" 做為 Index 引數,來抓取代表帳單生日的物件。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))

請注意,第一個呼叫會明確指定 Item 屬性,但第二個則否。Note that the first call explicitly specifies the Item property, but the second does not. 這兩個呼叫都有作用,因為 Item 屬性是 Collection 物件的預設屬性。Both calls work because the Item property is the default property for a Collection object.

備註

如果 Index 的類型 ObjectItem 屬性會嘗試將它視為 StringCharChar 陣列或整數值。If Index is of type Object, the Item property attempts to treat it as a String, Char, Char array, or integer value. 如果 Item 無法將 Index 轉換成 StringInteger,它會擲回 ArgumentException 例外狀況。If Item cannot convert Index to String or Integer, it throws an ArgumentException exception.

Item 屬性是集合的預設屬性。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)))  

Item[Object]

依位置或索引鍵取得 Collection 物件的特定元素。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; }
member this.Item(obj) : obj
Default Public ReadOnly Property Item(Index As Object) As Object

參數

Index
Object

數值運算式,其指定集合中項目的位置。A numeric expression that specifies the position of an element of the collection. Index 必須是從 1 到集合 Count 屬性值的一個數字。Index must be a number from 1 through the value of the collection's Count property. 或是 (B) Object 運算式,其指定集合中項目的位置或索引鍵字串。Or (B) An Object expression that specifies the position or key string of an element of the collection.

屬性值

依位置或索引鍵 Collection 物件的特定元素。A specific element of a Collection object either by position or by key.

範例

下列範例會使用 Item 屬性來抓取集合中物件的參考。The following example uses the Item property to retrieve a reference to an object in a collection. 它會建立 birthdays 做為 Collection 物件,然後使用索引鍵 "Bill" 做為 Index 引數,來抓取代表帳單生日的物件。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))

請注意,第一個呼叫會明確指定 Item 屬性,但第二個則否。Note that the first call explicitly specifies the Item property, but the second does not. 這兩個呼叫都有作用,因為 Item 屬性是 Collection 物件的預設屬性。Both calls work because the Item property is the default property for a Collection object.

備註

如果 Index 的類型 ObjectItem 屬性會嘗試將它視為 StringCharChar 陣列或整數值。If Index is of type Object, the Item property attempts to treat it as a String, Char, Char array, or integer value. 如果 Item 無法將 Index 轉換成 StringInteger,它會擲回 ArgumentException 例外狀況。If Item cannot convert Index to String or Integer, it throws an ArgumentException exception.

Item 屬性是集合的預設屬性。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)))  

Item[String]

依位置或索引鍵取得 Collection 物件的特定元素。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; }
member this.Item(string) : obj
Default Public ReadOnly Property Item(Key As String) As Object

參數

Key
String

唯一的 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 必須對應至當項目新增至集合時所指定的 Key 引數。Key must correspond to the Key argument specified when the element was added to the collection.

屬性值

依位置或索引鍵 Collection 物件的特定元素。A specific element of a Collection object either by position or by key.

範例

下列範例會使用 Item 屬性來抓取集合中物件的參考。The following example uses the Item property to retrieve a reference to an object in a collection. 它會建立 birthdays 做為 Collection 物件,然後使用索引鍵 "Bill" 做為 Index 引數,來抓取代表帳單生日的物件。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))

請注意,第一個呼叫會明確指定 Item 屬性,但第二個則否。Note that the first call explicitly specifies the Item property, but the second does not. 這兩個呼叫都有作用,因為 Item 屬性是 Collection 物件的預設屬性。Both calls work because the Item property is the default property for a Collection object.

備註

如果 Index 的類型 ObjectItem 屬性會嘗試將它視為 StringCharChar 陣列或整數值。If Index is of type Object, the Item property attempts to treat it as a String, Char, Char array, or integer value. 如果 Item 無法將 Index 轉換成 StringInteger,它會擲回 ArgumentException 例外狀況。If Item cannot convert Index to String or Integer, it throws an ArgumentException exception.

Item 屬性是集合的預設屬性。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)))  

適用於