Class

# Dictionary

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Description

An object that contains a list of *key-value* pairs. Both keys and values are `Variants</api/data_types/variant>`. ASCII `String</api/data_types/string>` keys are case-insensitive, but non-ASCII `String</api/data_types/string>` keys can be case-sensitive. `String</api/data_types/string>` keys are encoding-sensitive.

## Properties

<div class="rst-class">

table-centered_columns_3_and_4

</div>

| Name                            | Type                               | Read-Only | Shared |
|---------------------------------|------------------------------------|-----------|--------|
| `BinCount<dictionary.bincount>` | `Integer</api/data_types/integer>` |           |        |
| `KeyCount<dictionary.keycount>` | `Integer</api/data_types/integer>` | ✓         |        |

## Methods

<div class="rst-class">

table-centered_column_4

</div>

| Name                                   | Parameters                                                                                                              | Returns                              | Shared |
|----------------------------------------|-------------------------------------------------------------------------------------------------------------------------|--------------------------------------|--------|
| `Clone<dictionary.clone>`              |                                                                                                                         | Dictionary                           |        |
| `Constructor<dictionary.constructor0>` | keyComparison As `Dictionary.KeyComparisonDelegate<dictionary.keycomparisondelegate>`                                   |                                      |        |
| `Constructor<dictionary.constructor1>` | `ParamArray</api/language/paramarray>` entries As `Pair</api/language/pair>`                                            |                                      |        |
| `HasKey<dictionary.haskey>`            | key As `Variant</api/data_types/variant>`                                                                               | `Boolean</api/data_types/boolean>`   |        |
| `Iterator<dictionary.iterator>`        |                                                                                                                         | `Iterator</api/language/iterator>`   |        |
| `Key<dictionary.key>`                  | index As `Integer</api/data_types/integer>`                                                                             | `Variant</api/data_types/variant>`   |        |
| `Keys<dictionary.keys>`                |                                                                                                                         | `Variant()</api/data_types/variant>` |        |
| `Lookup<dictionary.lookup>`            | key As `Variant</api/data_types/variant>`, defaultValue As `Variant</api/data_types/variant>`                           | `Variant</api/data_types/variant>`   |        |
| `Remove<dictionary.remove>`            | key As `Variant</api/data_types/variant>`                                                                               |                                      |        |
| `RemoveAll<dictionary.removeall>`      |                                                                                                                         |                                      |        |
| `Value<dictionary.value>`              | key As `Variant</api/data_types/variant>`                                                                               | `Variant</api/data_types/variant>`   |        |
|                                        | key As `Variant</api/data_types/variant>`, `Assigns</api/language/assigns>` value As `Variant</api/data_types/variant>` |                                      |        |
| `Values<dictionary.values>`            |                                                                                                                         | `Variant()</api/data_types/variant>` |        |

## Delegate Methods

<div class="rst-class">

table-centered_column_4

</div>

| Name                                                      | Parameters                                                                                    | Returns                            |
|-----------------------------------------------------------|-----------------------------------------------------------------------------------------------|------------------------------------|
| `KeyComparisonDelegate<dictionary.keycomparisondelegate>` | leftKey As `Variant</api/data_types/variant>`, rightKey As `Variant</api/data_types/variant>` | `Integer</api/data_types/integer>` |

## Property descriptions

<div id="dictionary.bincount">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Dictionary.BinCount

**BinCount** As `Integer</api/data_types/integer>`

> The number of bins the hash table uses. This is a measure of the hash table size, independent of the number of items the <span class="title-ref">Dictionary</span> contains.
>
> Normally, you do not need to be concerned with bins and the hash table. BinCount would be of use to you only if you have very large dictionaries consisting of thousands of entries and you want to try to optimize performance.
>
> If you set BinCount to a positive value, the <span class="title-ref">Dictionary</span> will use that number of bins regardless of the number of items in the <span class="title-ref">Dictionary</span>.
>
> If you set BinCount to a value equal to or less than zero, the <span class="title-ref">Dictionary</span> will pick whatever value it thinks is appropriate for the number of items in the <span class="title-ref">Dictionary</span>. It will dynamically change the number of bins as items are added or removed from the <span class="title-ref">Dictionary</span> (or until you assign a positive value to BinCount).
>
> The latter is the default behavior.
>
> This example sets the number of bins:
>
> ``` xojo
> Var d As New Dictionary
> d.BinCount = 100
> ```

<div id="dictionary.keycount">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Dictionary.KeyCount

**KeyCount** As `Integer</api/data_types/integer>`

> The number of *key-value* pairs in the <span class="title-ref">Dictionary</span>.
>
> This property is read-only.
>
> Returns the actual Count, not the Ubound of an array.
>
> The following code displays the number of *key-value* pairs:
>
> ``` xojo
> Var d As New Dictionary
> ' display the entries for the dictionary
> MessageBox(d.KeyCount.ToString)
> ```

## Method descriptions

<div id="dictionary.clone">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Dictionary.Clone

**Clone** As <span class="title-ref">Dictionary</span>

> Returns a copy of the contents of the <span class="title-ref">Dictionary</span> allowing it to be manipulated independently of the original. Because any <span class="title-ref">Dictionary</span> items that are objects are really just references to the object, while the references themselves are duplicated, the objects they reference are not. Changing an object in one will change it in the other.
>
> In the example code below, the <span class="title-ref">Dictionary</span> (reservation1) is cloned into reservation2. The item "Name" in reservation2 is changed to `Samwise Gamgee` which leaves the "Name" value unchanged in reservation1. However, because a `DateTime</api/data_types/datetime>` is an object, the "Date" item in reservation2 is still the same as it is in reservation1 because it's a reference a `DateTime</api/data_types/datetime>` object and not a copy of it. In the last line of the example, a new `DateTime</api/data_types/datetime>` object is created using `DateTime.AddInterval<datetime.addinterval>` to create a `DateTime</api/data_types/datetime>` that is 1 year later. At that point, the two "Date" items point to two different `DateTime</api/data_types/datetime>` objects.
>
> ``` xojo
> Var today As DateTime = DateTime.Now
> Var reservation1 As New Dictionary
> reservation1.Value("Name") = "Frodo Baggins"
> reservation1.Value("Date") = today
>
> Var reservation2 As Dictionary
> reservation2 = reservation1.Clone
> reservation2.Value("Name") = "Samwise Gamgee"
>
> reservation2.Value("Date") = reservation2.Value("Date").DateTimeValue.AddInterval(1)
> ```
>
> If you prefer to copy objects referenced in a <span class="title-ref">Dictionary</span>, you will have to handle that yourself.
>
> For example, if the last line in the example above was replaced by the following line, the "Date" value would be a copy of the original rather than a reference to it.
>
> ``` xojo
> reservation2.Value("Date") = New DateTime(reservation1.Value("Date").DateTimeValue)
> ```

<div id="dictionary.constructor0">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Dictionary.Constructor

**Constructor**(keyComparison As `Dictionary.KeyComparisonDelegate<dictionary.keycomparisondelegate>`)

> <div class="note">
>
> <div class="title">
>
> Note
>
> </div>
>
> `Constructors</api/language/constructor>` are special methods called when you create an object with the `New</api/language/new>` keyword and pass in the parameters above.
>
> </div>
>
> Creates a <span class="title-ref">Dictionary</span> that gives you control over how key uniqueness is determined. For example, you'd use this if you wanted to create case-sensitive keys.
>
> <div class="important">
>
> <div class="title">
>
> Important
>
> </div>
>
> This constructor is not currently supported for Android.
>
> </div>

<div id="dictionary.constructor1">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Dictionary.Constructor

**Constructor**(`ParamArray</api/language/paramarray>` entries As `Pair</api/language/pair>`)

> The parameter is the list of `Pairs</api/language/pair>` that will make up the <span class="title-ref">Dictionary</span>.
>
> This example passes the specs for a rectangle as as a list of `Pairs</api/language/pair>`. The *key*s are Top, Left, Width, and Height and the values are the values that belong to each *key* value.
>
> ``` xojo
> Var d As New Dictionary("left" : 0, "top" : 10, "width" : 300, "height" : 300)
> ```

<div id="dictionary.haskey">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Dictionary.HasKey

**HasKey**(key As `Variant</api/data_types/variant>`) As `Boolean</api/data_types/boolean>`

> Returns `True</api/language/true>` if *key* is in the <span class="title-ref">Dictionary</span> and `False</api/language/false>` if it is not.
>
> Returns a `Boolean</api/data_types/boolean>`. The HasKey function is encoding-sensitive. The <span class="title-ref">Dictionary</span> works off hash functions and the hash of a UTF-16 string is vastly different from the hash of a UTF-8 string.
>
> This example checks to see if the key corresponding to the passed color is in the <span class="title-ref">Dictionary</span>.
>
> ``` xojo
> If d.HasKey(Color.Red) Then
>   MessageBox(d.Value(Color.Red))
> Else
>   MessageBox("The Red Key is not in the dictionary.")
> End If
> ```

<div id="dictionary.iterator">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Dictionary.Iterator

**Iterator** As `Iterator</api/language/iterator>`

> Allows iterating through all of the items in the <span class="title-ref">Dictionary</span> with `For...Each</api/language/loops/for_each...next>`.

<div id="dictionary.key">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Dictionary.Key

**Key**(index As `Integer</api/data_types/integer>`) As `Variant</api/data_types/variant>`

> Returns the value of *key* for the item in the <span class="title-ref">Dictionary</span> at the index passed.
>
> Keys are not case-sensitive, but they are encoding-sensitive. If there is no item in the <span class="title-ref">Dictionary</span> at the index passed, a call generates an `OutOfBoundsException</api/exceptions/outofboundsexception>` error.
>
> ``` xojo
> 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))( ' retrieves "This is pure blue."
> ```

<div id="dictionary.keys">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Dictionary.Keys

**Keys** As `Variant()</api/data_types/variant>`

> Returns an array of keys allowing you to iterate through all the key/value pairs in the <span class="title-ref">Dictionary</span>.
>
> The order is stable and matches the order returned by the `Values<dictionary.values>` method at least until the <span class="title-ref">Dictionary</span> is modified. Use this method with `For Each</api/language/loops/for_each...next>` or `For...Next</api/language/loops/for...next>` to loop through all the keys. Keys are not case-sensitive, but they are encoding-sensitive.
>
> Suppose you have a <span class="title-ref">Dictionary</span> wordCounts whose keys are words, and whose values are the number of occurrences of the key in some text. You can produce a list of words sorted by count as follows.
>
> ``` xojo
> Var word() As String
> Var count() As Integer
>
> For Each key As Variant In wordCounts.Keys
>   word.Add(key)
>   count.Add(wordCounts.Value(key))
> Next
>
> count.SortWith(word)
> ```

<div id="dictionary.lookup">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Dictionary.Lookup

**Lookup**(key As `Variant</api/data_types/variant>`, defaultValue As `Variant</api/data_types/variant>`) As `Variant</api/data_types/variant>`

> Looks up the passed value of *Key*. Returns a `Variant</api/data_types/variant>`.
>
> If *Key* is found, it returns the corresponding value. If *key* is not found, it returns the passed *defaultValue*.
>
> This example looks up the passed color in the <span class="title-ref">Dictionary</span>.
>
> ``` xojo
> Var d As New Dictionary
> d.Value(Color.Red) = "This is pure red."
> d.Value(Color.Blue) = "This is pure blue."
>
> Var a As Variant
> a = d.Lookup(Color.Red, "Default color")
> ```

<div id="dictionary.remove">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Dictionary.Remove

**Remove**(key As `Variant</api/data_types/variant>`)

> Removes the *key* <sub>th</sub> *value-key* pair from the <span class="title-ref">Dictionary</span>.
>
> If *key* is not in the <span class="title-ref">Dictionary</span>, it raises a `KeyNotFoundException</api/exceptions/keynotfoundexception>` error.
>
> The following code removes the passed entry from the \`Dictionary\`:
>
> ``` xojo
> Try
>   d.Remove(Color.Red)
> Catch err As KeyNotFoundException
>   MessageBox("You tried to access a nonexistent item!")
> End Try
> ```

<div id="dictionary.removeall">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Dictionary.RemoveAll

**RemoveAll**

> Removes all entries from the <span class="title-ref">Dictionary</span>. This invalidates all iterators that were created from the <span class="title-ref">Dictionary</span>.
>
> Remove all entries from a \`Dictionary\`:
>
> ``` xojo
> d1.RemoveAll
> ```

<div id="dictionary.value">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Dictionary.Value

**Value** value As `Variant</api/data_types/variant>`

> Retrieves a value to the *key* item in the <span class="title-ref">Dictionary</span>.

**Value**(value As `Variant</api/data_types/variant>`, `Assigns</api/language/assigns>` value As `Variant</api/language/assigns>`)

> Assigns a value to the *key* item in the <span class="title-ref">Dictionary</span>.
>
> The Value function is encoding-sensitive. The <span class="title-ref">Dictionary</span> works of hash functions and the hash of a UTF-16 string is vastly different than the hash of a UTF-8 string.
>
> If you attempt to read a value for a key that is not in the <span class="title-ref">Dictionary</span>, a `KeyNotFoundException</api/exceptions/keynotfoundexception>` error is raised. To avoid the exception, use the `Lookup<dictionary.lookup>` method to provide a default value if the key is not found or use the `HasKey<dictionary.haskey>` method to check if the key exists.
>
> The <span class="title-ref">Dictionary</span> example in the Examples folder allows you to enter and display <span class="title-ref">Dictionary</span> entries. It uses only strings for the key and value, but you can actually use any type that can be stored as a `Variant</api/data_types/variant>`. The fields *KeyField* and *ValueField* contain the new *key:value* pair.
>
> To use the example, enter a key and value pair and then press the “Set Value for Key” button. Its code is:
>
> ``` xojo
> ' add a value the same way you would set the value
> ' the key is a variant, which means it can be
> ' any data type.
> //
> ' the value is also a variant
> ' however, this example only uses strings for keys and values
>
> myDictionary.Value(KeyField.Text) = ValueField.Text
>
> UpdateListbox
> ```
>
> *UpdateListbox* is a window method and it repopulates the ListBox.
>
> ``` xojo
> ListBox1.RemoveAllRows
>
> For Each entry As DictionaryEntry In MyDictionary
>   ListBox1.AddRow(entry.Key)
> Next
> ```

<div id="dictionary.values">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Dictionary.Values

**Values** As `Variant()</api/data_types/variant>`

> Returns all the values in the <span class="title-ref">Dictionary</span> as an array of `Variants</api/data_types/variant>`.
>
> The order is stable and matches the order returned by Keys at least until the <span class="title-ref">Dictionary</span> is modified. Use this method with `For Each</api/language/loops/for_each...next>` to loop through all the values.
>
> The following code retrieves the entries in the <span class="title-ref">Dictionary</span> and populates an array of `Variants</api/data_types/variant>`:
>
> ``` xojo
> Var d As New Dictionary
> d.Value(1) = "123"
> d.Value(2) = "234"
> d.Value(3) = "123"
>
> Var v() As Variant
> v = d.Values ' after this line v will be an array of variants that are all the values in the dictionary
> ```

## Delegate descriptions

<div id="dictionary.keycomparisondelegate">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Dictionary.KeyComparisonDelegate

**KeyComparisonDelegate**(leftKey As `Variant</api/data_types/variant>`, rightKey As `Variant</api/data_types/variant>`) As `Integer</api/data_types/integer>`

> Implement this delegate if you would like the <span class="title-ref">Dictionary</span> to support case-sensitive keys. Return integer with value like String.Compare function.
>
> <div class="important">
>
> <div class="title">
>
> Important
>
> </div>
>
> This delegate is not supported for Android.
>
> </div>

## Interfaces

This class implements the `Iterable</api/language/iterable>` class interface.

## Notes

The <span class="title-ref">Dictionary</span> class provides the functionality of the `Collection</api/language/collection>` class and offers several advantages: With the `Collection</api/language/collection>` class, the time taken to locate an item is a function of the number of items in the `Collection</api/language/collection>` because the search is sequential. A <span class="title-ref">Dictionary</span> 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 <span class="title-ref">Dictionary</span> is a `Variant</api/data_types/variant>`, but is a `String</api/data_types/string>` in the `Collection</api/language/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 <span class="title-ref">Dictionary</span>, the new value replaces the old one

The `Pair</api/language/pair>` class also stores key-value items. The `Pair</api/language/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</api/language/pair>` class has only the two properties that contain the values of the pair.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

### Iterating through a <span class="title-ref">Dictionary</span>

To iterate through the contents of a <span class="title-ref">Dictionary</span>, use the `DictionaryEntry</api/language/dictionaryentry>` class.

### String key differences

Dictionaries use hashes for their lookup functions. Because of this, string keys need to match exactly; otherwise the <span class="title-ref">Dictionary</span> 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 <span class="title-ref">Dictionary</span> keys because of a case-insensitive hashing function. However, non-ASCII characters such as "é" and "É" are not treated as identical in <span class="title-ref">Dictionary</span> 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:

``` xojo
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,

``` xojo
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
```

### Case-Sensitive keys

While <span class="title-ref">Dictionary</span> keys are case-insensitive, you can create case-sensitive keys if you need them:

This function provides a case-sensitive comparison:

``` xojo
Public Function CaseSensitiveKeyComparisonDelegate(leftKey As Variant, rightKey As Variant) As Integer
  Return leftKey.StringValue.Compare(rightKey.StringValue, ComparisonOptions.CaseSensitive)
End Function
```

You then call the function above when creating a \`Dictionary\`:

``` xojo
Var d As New Dictionary(AddressOf CaseSensitiveKeyComparisonDelegate)

d.Value("Hello") = 123
d.Value("hello") = 234

Break ' The dictionary has now 2 entries in the dictionary
```

## Sample code

The following code takes advantage of the fact that the key is a `Variant</api/data_types/variant>`, and not necessarily a number. In this example, the keys are colors and the values are descriptions.

``` xojo
Try
  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."
Catch err As KeyNotFoundException
  MessageBox("Key not in the dictionary")
Catch err As OutOfBoundsException
  MessageBox("The index of the key is out of bounds!")
End Try
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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

``` xojo
MessageBox(d.Value(d.Key(1)))
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

is not an element in the <span class="title-ref">Dictionary</span>, an `OutOfBoundsException</api/exceptions/outofboundsexception>` occurs and the `Exception block</api/exceptions/exception>` at the end of the method will trap it. You could also retrieve this value in the <span class="title-ref">Dictionary</span> by passing the Value method the key rather than the key's index

``` xojo
MessageBox(d.Value(Color.Blue))
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

In this case, if you pass a key that is not in the <span class="title-ref">Dictionary</span>, a `KeyNotFoundException</api/exceptions/keynotfoundexception>` will occur and the `Exception block</api/exceptions/exception>` will also trap it and display the appropriate error message.

This code returns the value of `KeyCount<dictionary.keycount>` for the above code:

``` xojo
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</api/exceptions/exception>` block, you could first use the HasKey method before trying to access the value:

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

## Compatibility

|                       |     |
|-----------------------|-----|
| **Project Types**     | All |
| **Operating Systems** | All |

<div class="seealso">

`Object</api/data_types/additional_types/object>` parent class; `DictionaryEntry</api/language/dictionaryentry>`, `KeyNotFoundException</api/exceptions/keynotfoundexception>` error; `Collection</api/language/collection>`, `Pair</api/language/pair>`, and `Variant</api/data_types/variant>` classes

</div>
