Name
Dictionary Object
Creatable
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, which are called items or members, containing data. A Dictionary object can contain any data whatsoever, including objects and other Dictionary objects. You 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.
Actually, until recently, it was generally believed that
For
Each...Next
didn’t
work with the Dictionary object. But in fact, it does. 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 ...
Get VBScript in a Nutshell 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.