You are here:About>Computing & Technology>Visual Basic> Using VB.NET> VB 6 Collection, VBScript Dictionary, and VB .NET Hashtable
About.comVisual Basic
VB 6 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)

Fortunately, both VB 6 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 VB 6 first ...

In VB 6, 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 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 in VB 6 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 (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. But if you add a reference to the Scripting library, you can 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 Dictionary object is actually an early version of VB .NET's Hashtable object. We'll look at the new ideas built into the Hashtable object the next installment of this mini-tutorial.

From Dan Mabbutt,
Your Guide to Visual Basic.
FREE Newsletter. Sign Up Now!
Newsletters & RSSEmail to a friendSubmit to Digg
 All Topics | Email Article | | |
Advertising Info | News & Events | Work at About | SiteMap | Reprints | HelpOur Story | Be a Guide
User Agreement | Ethics Policy | Patent Info. | Privacy Policy©2008 About, Inc., A part of The New York Times Company. All rights reserved.