Contains 方法(Collection 对象)

更新:2007 年 11 月

返回一个 Boolean 值,该值指示 Visual Basic Collection 对象是否包含一个带有特定键的元素。

Public Function Contains( _
    ByVal Key As String _
) As Boolean

参数

  • Key
    必选。String 表达式,指定搜索集合的元素所用的键。

异常/错误代码

异常类型

错误号

条件

ArgumentException

5

指定的 Key 为 Nothing。

如果正在升级使用无结构错误处理的 Visual Basic 6.0 应用程序,请参见“错误号”一列。(您可以根据 Number 属性(Err 对象)比较错误号。)然而,如果可能,应当考虑用 Visual Basic 的结构化异常处理概述替换这种错误控制。

备注

如果集合包含的元素具有与 Key 完全匹配的键,则 Contains 返回 True。否则,Contains 返回 False。

Visual Basic Collection 可以保存具有键的一些元素和没有键的其他元素。这取决于对 Add 方法(Collection 对象)的调用是否为可选的 Key 参数提供了变量。

示例

Dim customers As New Microsoft.VisualBasic.Collection()
Dim accountNumber As String = "1234"
' Insert code that obtains new customer objects.
' Use the new customer's account number as the key.
customers.Add(newCustomer, accountNumber)
' The preceding statements can be repeated for several customers.
Dim searchNumber As String = "1234"
' Insert code to obtain an account number to search for.
If customers.Contains(searchNumber) Then
    MsgBox("The desired customer is in the collection.")
Else
    MsgBox("The desired customer is not in the collection.")
End If

如果要使用键在集合中搜索元素,请记住在每次调用 Add 方法时提供 Key 参数。

要求

命名空间:Microsoft.VisualBasic

**模块:**Collection

**程序集:**Visual Basic 运行库(在 Microsoft.VisualBasic.dll 中)

请参见

参考

Collection 对象 (Visual Basic)

Item 属性(Collection 对象)

Count 属性(Collection 对象)

Add 方法(Collection 对象)

Remove 方法(Collection 对象)

Clear 方法(Collection 对象)