Collection.Item[] 属性

定义

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

重载

名称 说明
Item[Int32]

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

Item[Object]

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

Item[String]

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

Item[Int32]

Source:
Collection.vb
Source:
Collection.vb
Source:
Collection.vb
Source:
Collection.vb
Source:
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 属性检索对集合中对象的引用。 它创建 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 属性,但第二个调用没有。 这两个Item调用都起作用,因为该Collection属性是对象的默认属性。

注解

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

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

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

适用于

Item[Object]

Source:
Collection.vb
Source:
Collection.vb
Source:
Collection.vb
Source:
Collection.vb
Source:
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 属性检索对集合中对象的引用。 它创建 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 属性,但第二个调用没有。 这两个Item调用都起作用,因为该Collection属性是对象的默认属性。

注解

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

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

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

适用于

Item[String]

Source:
Collection.vb
Source:
Collection.vb
Source:
Collection.vb
Source:
Collection.vb
Source:
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 属性检索对集合中对象的引用。 它创建 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 属性,但第二个调用没有。 这两个Item调用都起作用,因为该Collection属性是对象的默认属性。

注解

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

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

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

适用于