Collection.Item[] 属性

定义

按位置或键获取 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; }
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.

属性值

Object

对象的特定元素( 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 属性是对象的默认属性,所以这两个调用都有效 CollectionBoth calls work because the Item property is the default property for a Collection object.

注解

如果 Index 的类型为 Object ,则 Item 属性将尝试将其视为 StringCharChar 数组或整数值。If Index is of type Object, the Item property attempts to treat it as a String, Char, Char array, or integer value. 如果 Item 无法转换 IndexStringInteger ,则会引发 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; }
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.

属性值

Object

对象的特定元素( 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 属性是对象的默认属性,所以这两个调用都有效 CollectionBoth calls work because the Item property is the default property for a Collection object.

注解

如果 Index 的类型为 Object ,则 Item 属性将尝试将其视为 StringCharChar 数组或整数值。If Index is of type Object, the Item property attempts to treat it as a String, Char, Char array, or integer value. 如果 Item 无法转换 IndexStringInteger ,则会引发 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; }
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.

属性值

Object

对象的特定元素( 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 属性是对象的默认属性,所以这两个调用都有效 CollectionBoth calls work because the Item property is the default property for a Collection object.

注解

如果 Index 的类型为 Object ,则 Item 属性将尝试将其视为 StringCharChar 数组或整数值。If Index is of type Object, the Item property attempts to treat it as a String, Char, Char array, or integer value. 如果 Item 无法转换 IndexStringInteger ,则会引发 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)))  

适用于