1. Technology

Your suggestion is on its way!

An email with a link to:

http://visualbasic.about.com/od/usingvbnet/l/aa070903a.htm

was emailed to:

Thanks for sharing About.com with others!

VB6 Collection, VBScript Dictionary, and VB.NET Hashtable
The VB 6 Collection object
The Scripting Dictionary object
The VB .NET Hashtable object
Explained!

Although most of the functions of VB.NET's Hashtable object can be done using the VB 6 Collection object, and even more can be done using the Scripting library's Dictionary object, VB.NET has vastly expanded and upgraded the way they work. In this mini-tutorial, we'll go over the idea of collections, dictionaries, and hashtables and then demonstrate those ideas with code.


The requirement to save information in an array is common in programming problems. One typical array problem is something like this:

Array Problem

Given the codes for parts, (Th, Do, and Gi), find the expanded name (Thingy, Dohicky, and Gizmo).

Of course, this is the sort of problem that databases were created to solve, but let's assume for a moment that a database is not appropriate for some reason. Another way to do it is to use similar arrays and simply search for matches.

Result
Dim strPartCode As String
strPartCode = InputBox("Enter the Part Code:")
I = 1
Do While strPartCode <> PartCodeArray(I) _
    And I < UBound(PartCodeArray)
    I = I + 1
Loop
If I = UBound(PartCodeArray) Then
    MsgBox ("The Part Code: " _
        & strPartCode _
        & " could not be found")
Else
    MsgBox ("Part Code: " _
        & strPartCode _
        & " -- Expanded Name: " _
        & PartNameArray(I))
End If

This approach, while it has the advantages of simplicity, also has some serious problems. Here's just a few:

  • It's inefficient and slow, especially when the arrays that need to be searched become larger.
  • You have to keep the arrays synchronized. The only thing that ties Code Do and Name Dohicky together is that they are both stored in array element 1.
  • You have to either code dynamic arrays, or guess about the maximum size that you will need.

What might work better is something like this code:

PartName = PartCode(strPartCode)

This is usually called a dictionary lookup. Fortunately, both VB6 and VB.NET have this functionality but (surprise!!!) VB.NET changes the way it works a lot. Let's see how we can create a program that works like the statement above in VB6.

In VB6, the object that does this is the Collection object. Collection object gives you a way to refer to a related group of items as a single object. Collections are used a lot by VB.NET itself (components in a project, for example, are a collection) and are pretty flexible. The members in a collection don't even have to be the same data type. Here's the way the same program would be coded with a collection object. The running forms look exactly the same as before.

Dim strPartCode As String
Dim strPartName As String
strPartCode = InputBox("Enter the Part Code:")
On Error GoTo NoPart
strPartName = PartName(strPartCode)
MsgBox ("Part Code: " _
    & strPartCode _
    & " -- Expanded Name: " _
    & strPartName)
Exit Sub
NoPart:
MsgBox ("The Part Code: " _
    & strPartCode _
    & " could not be found")
End Sub

You have to declare the collection as an object ...

Dim PartName As New Collection

and fill it with the Add method.

PartName.Add Item:="Thingy", Key:="Th"

Arrays vs Collections Collections can be as efficient as arrays. In a quick 10,000 iteration test, the collections beat the arrays (see results at left). But collections have only four methods in VB6(Add, Count, Item, and Remove) which sometimes makes it hard to do things. For example, the Error condition had to be used in our code earlier just to see if the collection contained the member that we were looking for.

Microsoft also supplies another object, called the Dictionary object, which does just about the same thing. This was added to the Microsoft Scripting object in 1996 as part of VB Script 2 to meet the competitive pressure of the Perl's HASH object. In VB.NET, you can add a reference to the Scripting library and use it in much the same way.

Scripting Reference

Here's the way that code looks in action:

Dim strPartCode As String
strPartCode = InputBox("Enter the Part Code:")
strPartName = d.Item(strPartCode)
If strPartName = "" Then
    MsgBox ("The Part Code: " _
        & strPartCode _
        & " could not be found")
Else
    MsgBox ("Part Code: " _
        & strPartCode _
        & " -- Expanded Name: " _
        & strPartName)
End If

And, again, you have to declare the object:

Dim d As New Dictionary

CompareMode The Dictionary object adds quite a bit of additional functionality. Note that we didn't have to use the Error condition to find out whether a member existed in the Dictionary object. The Dictionary object's Exists method could also have been used. A new CompareMode property also gives us a choice of matching methods.

Dictionary Performance

In Addition, in our 10,000 iteration test, it turns in a significantly better time!

The Scripting Dictionary object is actually an early version of VB.NET's Hashtable object. We'll look at the Hashtable object the next installment of this mini-tutorial. But in addition, when Microsoft introduced generics (see: Generics! Cleaner Data - Faster Code!, they also provided a completely new Dictionary object for VB.NET which makes the old Scripting version quite obsolete in VB.NET.

©2014 About.com. All rights reserved.