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