Collection.Item[] 属性

定义

按位置或键获取 Collection 对象的特定元素。

重载

Item[Int32]

按位置或键获取 Collection 对象的特定元素。

Item[Object]

按位置或键获取 Collection 对象的特定元素。

Item[String]

按位置或键获取 Collection 对象的特定元素。

Item[Int32]

按位置或键获取 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 表达式。

属性值

Object

按位置或键指定对象的特定元素 Collection

示例

以下示例使用 Item 属性检索对集合中对象的引用。 它将创建 birthdays 为对象 Collection ,然后使用键 "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 属性,但第二个调用不会。 这两个Collection调用都起作用,因为该Item属性是对象的默认属性。

注解

如果Index为类型Object,则Item属性会尝试将其视为整数String值、Char``Char数组或整数值。 如果Item无法转换为Index``StringInteger引发ArgumentException异常。

Item 属性是集合的默认属性。 因此,以下代码行等效。

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

适用于

Item[Object]

按位置或键获取 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 表达式。

属性值

Object

按位置或键指定对象的特定元素 Collection

示例

以下示例使用 Item 属性检索对集合中对象的引用。 它将创建 birthdays 为对象 Collection ,然后使用键 "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 属性,但第二个调用不会。 这两个Collection调用都起作用,因为该Item属性是对象的默认属性。

注解

如果Index为类型Object,则Item属性会尝试将其视为整数String值、Char``Char数组或整数值。 如果Item无法转换为Index``StringInteger引发ArgumentException异常。

Item 属性是集合的默认属性。 因此,以下代码行等效。

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

适用于

Item[String]

按位置或键获取 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 参数。

属性值

Object

按位置或键指定对象的特定元素 Collection

示例

以下示例使用 Item 属性检索对集合中对象的引用。 它将创建 birthdays 为对象 Collection ,然后使用键 "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 属性,但第二个调用不会。 这两个Collection调用都起作用,因为该Item属性是对象的默认属性。

注解

如果Index为类型Object,则Item属性会尝试将其视为整数String值、Char``Char数组或整数值。 如果Item无法转换为Index``StringInteger引发ArgumentException异常。

Item 属性是集合的默认属性。 因此,以下代码行等效。

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

适用于