CompareMode 方法

設定並傳回比較模式,以比較 Dictionary 物件中的字串索引鍵。

語法

object.CompareMode[ = compare]

引數:

  • Object必要。一律是 Dictionary 物件的名稱。

  • Compare選擇性。如有提供,則 Compare 是代表比較模式的值。可接受的值為 0 (二進位)、1 (文字)、2 (資料庫)。大於 2 的值可用來參照使用特定地區設定 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