| 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: 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. This approach, while it has the advantages of simplicity, also has some serious problems. Here's just a few: What might work better is something like this code: 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. 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" 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. Here's the way that code looks in action: And, again, you have to declare the object: Dim d As New Dictionary 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. |
|||||

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.
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.
