Using the Dictionary Object
by Alex Dinu
December 6, 1999
What is it and why use it?
Visual Basic and VB Script were never any good working with large
arrays and lookup tables. So the Microsoft ASP team decided to create a component that
will be the equivalent to the Perl hash, or associative array. It first came
out in 1996 as part of VB Script 2 and was added to the VB Scripting run-time library
(scrrun.dll) to enable VBScript programmers to use associative arrays. Now you can develop
in Visual Basic or VB Script just as you would in JScript and Perl. For more information
read this very
nice and interesting article 
OK, but what is it actually used for?
I was given a problem, not too long ago, that required me to
compare two very large sets of data. They were both over 300,000 lines.
No problem, I said, give me a couple of minutes, and I will then
give you the differences between the two lists. The data I needed to compare was a simple
one, a key and a value. I was required to first report of any keys found in one list and
not in the other, and if the key was there, I then had to make sure that the values were
also the same.
I quickly created a multidimensional array for each list,
populated the arrays, which took a while, but then adding 300,000 values into a
multidimensional is not a quick process, and started the comparison. Needless to say, it
didnt return me any results my machine died on me before any results were
displayed.
I went back and said that it will take me a little longer, and
that I needed more memory, when I remembered about the dictionary object. This method was
not only quicker, but also easier (for me that is) to use, and Im not talking about
the MS Word dictionary here.
A dictionary holds two sets of information, a unique key and a
value associated with the key, so we use the key to retrieve the item. The key can be
anything except a variant, but its should usually be an integer or a string. The
item can be of any type.
Before I bore you with all the details, let us start up Visual
Basic, and create a new EXE project.
Add the dictionary object references by opening the project
references from the Project menu and adding the Microsoft Scripting Runtime. The following
diagram should help you out with this.

The next step would be to define and declare the dictionary
object.
Dim Dict1 As Dictionary
The next step is to create the dictionary and populate it with
keys and items.
Set Dict1 = New Dictionary
With Dict1
.CompareMode = BinaryCompare
.Add 1, "Item 1"
.Add 2, "Item 2"
.Add 3, "Item 3"
End With
We now have a dictionary object that holds 3 rows with key values
1 to 3 and their associated item values. The Compare Mode sets and returns the comparison
mode for comparing string keys in a Dictionary object. The values can be vbBinaryCompare,
vbTextCompare or vbDatabaseCompare. Remember to set the Compare Mode before you add any
data to the dictionary otherwise you will get an error.
So now we have a dictionary object, containing some data ready to
be accessed. Lets take a look at a couple of methods we have available, to look up keys
and items within a dictionary object.
The Exists method is used to find a specific key in the
dictionary object. Say for example that you want to find out if you have the object
contains a key with a value of 1999. We use the following syntax to get either a true or
false:
Dim bReturnValue As Boolean
bReturnValue = Dict1.Exists(1999)
In this case, it will return False.
If you wish to get the item of a specific key, use the Items
method, which has the following syntax:
Dim vItemValue As Variant
vItemValue = Dict1.Item(3)
Which in this case will return you "Item 3".
Lets take a look now at how we could compare two dictionaries,
find any items that are in one and not the other, and if any key items are different.
First, lets populate tow dictionary objects with keys and items.
Notice that by using the following code, we have 3 differences: key 1 is different, key 4
is not in Dict1 and key 3 is not in Dict2.
Dim Dict1 As Dictionary
Dim Dict2 As Dictionary
Dim vContainer1
Dim vContainer2
Set Dict1 = New Dictionary
Set Dict2 = New Dictionary
With Dict1
.CompareMode = BinaryCompare
.Add 1, "Item 1"
.Add 2, "Item 2"
.Add 3, "Item 3"
End With
With Dict2
.CompareMode = BinaryCompare
.Add 1, "Item 1a"
.Add 2, "Item 2"
.Add 3, "Item 4"
End With
For Each vContainer1 In Dict1
If Not Dict2.Exists(vContainer1) Then
Debug.Print vContainer1 & " is in Dict1 but not in Dict2"
Else
If Dict2.Item(vContainer1) <> Dict1.Item(vContainer1) Then
Debug.Print "Key item " & vContainer1 &
" is different"
End If
End If
Next
The results of the above code is:
Key item 1 is different
3 is in Dict1 but not in Dict2
Remember that we now have to step through each Dict2 line to find
the last difference we forced, but Im sure you can do that.
Conclusion
Many of you would be asking, why not use an array. Well, try and
do this with a multidimensional array with 300,000 lines of data, and you would see
straight away why. It takes roughly 2-3 minutes to add 300,000 lines of data into a
dictionary, and 4-5 minutes to compare the two dictionaries, depending on your PC type and
memory.
About the author
Alex Dinu is a specialist in Visual Basic, SQL Server, ASP, UML
and component design and development.
|