Name

Dictionary Object

Createable

Yes

Library

Microsoft Scripting Runtime

Description

The Dictionary object is similar to a Collection object, except that it’s loosely based on the Perl associative array. Like an array or a Collection object, the Dictionary object holds elements, called items or members, containing data. A Dictionary object can contain any data whatsoever, including objects and other Dictionary objects. Access the value of these dictionary items by using unique keys (or named values) that are stored along with the data, rather than by using an item’s ordinal position as you do with an array. This makes the Dictionary object ideal when you need to access data that is associated with a unique named value.

You can access each item stored to a Dictionary object by using the For Each ...Next construct. However, rather than returning a variant containing the data value stored to the Dictionary object as you would expect, it returns a variant containing the key associated with the member. You then have to pass this key to the Item method to retrieve the member, as the following example shows:

Dim vKey 
Dim sItem, sMsg 
Dim oDict 
    
Set oDict =  CreateObject("Scripting.Dictionary")
oDict.Add "One", "Engine"
oDict.Add "Two", "Wheel"
oDict.Add "Three", "Tire"
oDict.Add "Four", "Spanner"
        
For Each vKey In oDict
   sItem = oDict.Item(vKey)
   sMsg = sMsg & sItem & vbCrLf
Next
   
MsgBox sMsg

Dictionary Object Properties

The Dictionary object includes the following four properties:

Property

Description ...

Get VBScript in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.