You are currently offline, waiting for your internet to reconnect

How To Use the Dictionary Object with Visual Basic

This article was previously published under Q187234
This article discusses the use of a component from the Microsoft ScriptingLibrary; the Dictionary Object.
The Dictionary is quite similar to the Collection object in bothfunctionality and purpose. The Dictionary, however, offers somefunctionality that is not available with a Collection. Some of thesefeatures include:
  • The option to specify a comparison method for Keys. This allows for a case-sensitive Key, for example.
  • A method for determining if an object exists in a Dictionary.
  • A method for extracting all of the Keys into an Array.
  • A method for extracting all of the Items into an Array.
  • A method for changing a Key value.
  • A method for removing all items from the Dictionary.
  • Dictionary Keys are not limited to String datatype.
NOTE: One important difference between the Collection object and theDictionary object is the behavior of the Item property. If you use theItem property to reference a nonexistent Key in a Collection, you willreceive an error. If you use the Item property to reference a nonexistentKey in a Dictionary, that Key will be added to the Dictionary. Use theExists method to determine whether or not a Key is present in aDictionary.

The Dictionary object is a component of the Microsoft Scripting library,which does not ship with Visual Basic version 5.0 or later. You can obtainthe Microsoft Scripting library (SCRRUN.DLL) by installing one of thefollowing packages:
   Windows Script Host   Windows NT Option Pack   IIS 3.0   Scripting 3.1 upgrade.				

Step-by-Step Example

  1. Start a new Standard EXE project in Visual Basic. (Make sure that the Immediate Window is displayed.) Form1 is created by default.
  2. Add a reference to Microsoft Scripting Runtime.
  3. Add a standard module to the project.
  4. From the Project menu, select Project1.Properties and change the Startup object to Sub Main.
  5. Insert the following code into Module1:
          Option Explicit      Dim dict As Dictionary      Sub Main()      Dim keyArray, itemArray, element      Set dict = New Dictionary      With dict         'set compare mode         .CompareMode = BinaryCompare         'add item using named arguments         .Add Key:="mike", Item:=22         'add item without named arguments         .Add "joe", 33         'case sensitivity and Exists method         'does MIKE exist?         Debug.Print "MIKE exists = " & .Exists("MIKE")         'change key value         .Key("mike") = "MIKE"         'does MIKE exist?         Debug.Print "MIKE exists = " & .Exists("MIKE")         'extract keys into variant array         Debug.Print "Array of Keys"         keyArray = .Keys         For Each element In keyArray            Debug.Print element         Next         'extract items into variant array         Debug.Print "Array of Items"         itemArray = .Items         For Each element In itemArray            Debug.Print element         Next         'empty the dictionary         .RemoveAll         Debug.Print dict.Count & " Items in Dictionary"      End With      Set dict = Nothing      End Sub					
  6. Run the project. You should see that the initial search for the key fails, then succeeds after the key value is replaced with an uppercase value. Next, you should see the contents of the variant arrays that were extracted from the dictionary using the Keys and Items methods. Last, you see the result of emptying the Dictionary.

Article ID: 187234 - Last Review: 07/13/2004 14:59:00 - Revision: 2.1

  • Microsoft Visual Basic 5.0 Learning Edition
  • Microsoft Visual Basic 6.0 Learning Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic 6.0 Enterprise Edition
  • kbhowto kbscript KB187234