Collection.Item[] 屬性

定義

依位置或索引鍵取得 Collection 物件的特定元素。

多載

Item[Int32]

依位置或索引鍵取得 Collection 物件的特定元素。

Item[Object]

依位置或索引鍵取得 Collection 物件的特定元素。

Item[String]

依位置或索引鍵取得 Collection 物件的特定元素。

Item[Int32]

來源:
Collection.vb
來源:
Collection.vb
來源:
Collection.vb

依位置或索引鍵取得 Collection 物件的特定元素。

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

參數

Index
Int32

數值運算式,其指定集合中項目的位置。 Index 必須是從 1 到集合 Count 屬性值的一個數字。 或是 (B) Object 運算式,其指定集合中項目的位置或索引鍵字串。

屬性值

依位置或索引鍵指定物件的特定專案 Collection

範例

下列範例會 Item 使用 屬性來擷取集合中對象的參考。 它會建立 birthdaysCollection 對象,然後使用索引鍵 "Bill" 做為 Index 自變數,擷取代表 Bill 生日的物件。

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 屬性,但第二個呼叫則不會。 這兩個呼叫都會運作, Item 因為 屬性是 對象的默認屬性 Collection

備註

如果 Index 型別為 Object,則Item屬性會嘗試將它String視為 、、CharChar陣列或整數值。 如果 Item 無法轉換成 IndexStringInteger,則會擲回例外狀況 ArgumentException

屬性 Item 是集合的預設屬性。 因此,下列幾行程式代碼是相等的。

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

適用於

Item[Object]

來源:
Collection.vb
來源:
Collection.vb
來源:
Collection.vb

依位置或索引鍵取得 Collection 物件的特定元素。

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

參數

Index
Object

數值運算式,其指定集合中項目的位置。 Index 必須是從 1 到集合 Count 屬性值的一個數字。 或是 (B) Object 運算式,其指定集合中項目的位置或索引鍵字串。

屬性值

依位置或索引鍵指定物件的特定專案 Collection

範例

下列範例會 Item 使用 屬性來擷取集合中對象的參考。 它會建立 birthdaysCollection 對象,然後使用索引鍵 "Bill" 做為 Index 自變數,擷取代表 Bill 生日的物件。

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 屬性,但第二個呼叫則不會。 這兩個呼叫都會運作, Item 因為 屬性是 對象的默認屬性 Collection

備註

如果 Index 型別為 Object,則Item屬性會嘗試將它String視為 、、CharChar陣列或整數值。 如果 Item 無法轉換成 IndexStringInteger,則會擲回例外狀況 ArgumentException

屬性 Item 是集合的預設屬性。 因此,下列幾行程式代碼是相等的。

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

適用於

Item[String]

來源:
Collection.vb
來源:
Collection.vb
來源:
Collection.vb

依位置或索引鍵取得 Collection 物件的特定元素。

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

參數

Key
String

唯一的 String 運算式,其指定用來存取集合中項目的索引鍵字串,藉以取代位置索引。 Key 必須對應至當項目新增至集合時所指定的 Key 引數。

屬性值

依位置或索引鍵指定物件的特定專案 Collection

範例

下列範例會 Item 使用 屬性來擷取集合中對象的參考。 它會建立 birthdaysCollection 對象,然後使用索引鍵 "Bill" 做為 Index 自變數,擷取代表 Bill 生日的物件。

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 屬性,但第二個呼叫則不會。 這兩個呼叫都會運作, Item 因為 屬性是 對象的默認屬性 Collection

備註

如果 Index 型別為 Object,則Item屬性會嘗試將它String視為 、、CharChar陣列或整數值。 如果 Item 無法轉換成 IndexStringInteger,則會擲回例外狀況 ArgumentException

屬性 Item 是集合的預設屬性。 因此,下列幾行程式代碼是相等的。

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

適用於