DOCUMENT:Q187234 08-JUN-1998 [vbwin] TITLE :HOWTO: Use the Dictionary Object with Visual Basic PRODUCT :Microsoft Visual Basic for Windows PROD/VER:WINDOWS:5.0 OPER/SYS:WINDOWS KEYWORDS: ====================================================================== --------------------------------------------------------------------- The information in this article applies to: - Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, version 5.0 --------------------------------------------------------------------- SUMMARY ======= This article discusses the use of a component from the Microsoft Scripting Library; the Dictionary Object. MORE INFORMATION ================ The Dictionary is quite similar to the Collection object in both functionality and purpose. The Dictionary, however, offers some functionality that is not available with a Collection. Some of these features 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. The Dictionary object is a component of the Microsoft Scripting library, which does not ship with Visual Basic version 5.0. You can obtain the Microsoft Scripting library (SCRRUN.DLL) by installing one of the following packages: Windows Scripting 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 Windows 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. Additional query words: kbDSupport kbDSD kbVBA kbVBp500 ====================================================================== Version : WINDOWS:5.0 Platform : WINDOWS Issue type : kbhowto ============================================================================= THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY. Copyright Microsoft Corporation 1998.