CompareMode 方法

设置并返回用于比较 Dictionary 对象中字符串键的比较模式。

语法

object.CompareMode[ = compare]

参数:

  • Object必需。始终为 Dictionary 对象的名称。

  • Compare可选。如果提供,则 compare 是表示比较模式的值。可接受的值为 0(二进制)、1(文本)、2(数据库)。大于 2 的值可用于引用使用特定区域设置 ID (Locale ID, LCID) 的比较。

注释

如果尝试更改已包含数据的 Dictionary 对象的比较模式,则会出现错误。

以下示例说明了 CompareMode 属性的用法:

示例 1:

Sub TextCompareExample
    Dim d   ' Create a variable.
    Set d = CreateObject("Scripting.Dictionary")

    ' Set comparison mode to Text (case-insensitive)
    d.CompareMode = vbTextCompare

    ' Add some keys and items.
    d.Add "a", "Athens"
    d.Add "b", "Belgrade"
    d.Add "c", "Cairo"

    ' Try to add a key that differs only in case
    On Error Resume Next
    d.Add "B", "Baltimore"
    Number = Err.Number
    If Number <> 0 Then
              Msg = "Error: Key 'b' already exists."
        Err.Clear
    End If
    On Error GoTo 0
 End Sub
' Output: Error: Key 'b' already exists.

示例 2:

Sub BinaryCompareExample
    Dim d   ' Create a variable.
    Set d = CreateObject("Scripting.Dictionary")

    ' Set comparison mode to Binary (case-sensitive)
    d.CompareMode = vbBinaryCompare

    ' Add some keys and items.
    d.Add "x", "Xenon"
    d.Add "y", "Yttrium"
    d.Add "z", "Zirconium"

    ' Try to add a key "X" which is different from "x"
    d.Add "X", "X-Ray"
 End Sub
' Output: Dictionary Object Contain Keys: x,y,z,X Items: Xenon,Yttrium,Zirconium,X-Ray