Metodo CompareMode

Imposta e restituisce la modalità di confronto per il confronto delle chiavi di tipo String in un oggetto Dictionary.

Sintassi

object.CompareMode[ = compare]

Argomenti:

  • Object: obbligatorio. Sempre il nome dell'oggetto Dictionary.

  • Compare: facoltativo. Se specificato, compare è un valore che rappresenta la modalità di confronto. I valori accettabili sono 0 (Binario), 1 (Testo), 2 (Database). È possibile utilizzare valori maggiori di 2 per fare riferimento ai confronti che utilizzano ID di impostazioni nazionali (LCID) specifici.

Note

Si verifica un errore se si tenta di modificare la modalità di confronto di un oggetto Dictionary che contiene già dati.

Nell'esempio seguente viene illustrato l'uso della proprietà CompareMode.

Esempio 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.

Esempio 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