From Xojo Documentation

You are currently browsing the old Xojo documentation site. It will go offline as of October 2, 2023. Please visit the new Xojo documentation site! - you will be redirected shortly...

Class (inherits from Object)

An object that contains a list of key-value pairs. Both keys and values are Variants. ASCII String keys are case-insensitive, but non-ASCII String keys can be case-sensitive. String keys are encoding-sensitive.

BinCount KeyCount fa-lock-32.png
Clone Keys RemoveAll
HasKey Lookup Value
Key Remove Values

Constructor(keyComparison As Dictionary.KeyComparisonDelegate)

Constructor(ParamArray entries as Pair)

Delegate Methods


The Dictionary class provides the functionality of the Collection class and offers several advantages: With the Collection class, the time taken to locate an item is a function of the number of items in the Collection because the search is sequential. A Dictionary uses a hash table, making the time (relatively) independent of the number of items. It is designed for high-speed lookups. Also, the key parameter in a Dictionary is a Variant, but is a String in the Collection class, giving you greater flexibility. On the other hand, the Collection can store multiple values per key. When you assign a value to a key that is already in the Dictionary, the new value replaces the old one

The Pair class also stores key-value items. The Pair class stores the key-value items in its Left and Right properties and an array of pairs can be set up as a linked list. The Pair class has only the two properties that contain the values of the pair.

String Key Differences

Dictionaries use hashes for their lookup functions. Because of this, string keys need to match exactly; otherwise the dictionary will not consider them equal. The only exception to this rule is with regard to string case. The characters "a" and "A" are treated as identical in dictionary keys because of a case-insensitive hashing function. However, non-ASCII characters such as "é" and "É" are not treated as identical in dictionary keys, even though they are equal in a direct string comparison.

While a UTF-16 string can be compared to a UTF-8 function in your code, they will provide different hashes when working with dictionaries.

Although the keys are case-insensitive, the case of the key is remembered. And the case of the key is not changed if a subsequent assignment uses a different case for the key. For example:

Var d As New Dictionary
d.Value("a") = "lower"
d.Value("A") = "UPPER"

MessageBox(d.Value("a")) // Displays "UPPER"
MessageBox(d.Value("A")) // Displays "UPPER"

// The actual key value is "a" as you can test using the Key method:
MessageBox(d.Key(0)) // Displays "a"

Integer Key Differences

Due to a particularity of Variant comparison, it is possible that Integer keys of different types may not evaluate as equivalent. For example,

Var v1 As Int32 = -1
Var v2 As Int64 = -1

Var d As New Dictionary
d.Value(v1) = "something"
If d.HasKey(v2) Then
// Won't get here, because the two keys are not considered equivalent
End If

Sample Code

The following code takes advantage of the fact that the key is a Variant, and not necessarily a number. In this example, the keys are colors and the values are descriptions.

Var d As New Dictionary
d.Value(Color.Red) = "This is pure red."
d.Value(Color.Blue) = "This is pure blue."
MessageBox(d.Value(d.Key(1))) // displays "This is pure blue."
Exception err As RuntimeException
If err IsA KeyNotFoundException Then
MessageBox("Key not in the dictionary")
End If
If err IsA OutOfBoundsException Then
MessageBox("The index of the key is out of bounds!")
End If

If the index passed to the Key method in the line:


is not an element in the dictionary, an OutOfBoundsException occurs and the Exception block at the end of the method will trap it. You could also retrieve this value in the dictionary by passing the Value method the key rather than the key's index


In this case, if you pass a key that is not in the dictionary, a KeyNotFoundException will occur and the Exception block will also trap it and display the appropriate error message.

This code returns the value of KeyCount for the above code:

Var d As New Dictionary
d.Value(Color.Red) = "This is pure red."
d.Value(Color.Blue) = "This is pure blue."
MessageBox(d.KeyCount.ToString) // displays "2"

Instead of the Exception block, you could first use the HasKey method before trying to access the value:

If d.HasKey(Color.Red) Then
MessageBox("Key not in the dictionary!")
End If

This code demonstrates how to use the Dictionary iterator:

Var d As New Dictionary
d.Value("First") = "Rosa"
d.Value("Last") = "Parks"

For Each de As DictionaryEntry In d
MessageBox(de.Key + " = " + de.Value)

See Also

KeyNotFoundException error; Collection, Pair, and Variant classes