Dictionary

From Real Software Documentation

Jump to: navigation, search
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.


Properties
BinCount Value
Count


Methods
Clear Lookup
HasKey Remove
Key Values
Keys


Constructors

Dictionary.Constructor(ParamArray entries as Pair)


Notes

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.

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 Real Studio, they will provide different hashes when working with dictionaries.

Examples

The following example 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.

Dim d as New Dictionary
d.Value(RGB(255,0,0))="This is pure red."
d.Value(RGB(0,255,255))="This is pure cyan."
MsgBox d.value(d.Key(1)) //retrieves "This is pure cyan."
Exception err as RuntimeException
If err IsA KeyNotFoundException then
MsgBox "Key not in the dictionary"
End if
If err IsA OutOfBoundsException then
MsgBox "The index of the key is out of bounds!"
End if


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

MsgBox d.value(d.Key(1))


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

MsgBox d.value(RGB(0,255,255))


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 Count for the above example:

Dim d as New Dictionary
d.Value(RGB(255,0,0))="This is pure red."
d.Value(RGB(0,255,255))="This is pure cyan."
MsgBox d.Count //returns 2


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

If d.HasKey(RGB(0,255,255)) then
MsgBox d.value(RGB(0,255,255))
Else
MsgBox "Key not in the dictionary!"
End If


See Also

KeyNotFoundException error; Collection, Pair, Variant classes.

Personal tools