Name
Dictionary.Add Method
Syntax
dictionaryobject
.Addkey
,item
-
dictionaryobject
Use: Required
Data Subtype: Dictionary object
A reference to a Dictionary object.
-
key
Use: Required
Data Subtype: String
A key value that’s unique in the Dictionary object.
-
item
Use: Required
Data Subtype: Any
The item to be added to the dictionary.
Description
Adds a key and its associated item to the specified Dictionary object.
Rules at a Glance
If the key isn’t unique, runtime error 457, “This key is already associated with an element of this collection,” is generated.
item
can be of any datatype, including objects and other Dictionary objects.
Example
This example creates an array to hold contact information, then adds the members of the array to the Dictionary object:
Dim oDict Dim sName Dim aContact(1) ' Contact array Set oDict = CreateObject("Scripting.Dictionary") sName = "Russell" ' Name aContact(0) = 10 ' Age aContact(1) = "0112 31234" ' Phone oDict.Add sName, aContact Msgbox oDict.Item(sName)(1) Set oDict = Nothing
Programming Tips & Gotchas
The order of members within a Dictionary object is officially undefined. That is, you can’t control the position of individual members, nor can you retrieve individual members based on their position within the Dictionary object. Your code, in short, should make no assumptions about the position of individual elements within the Dictionary objects.
Once you add a key and its associated data item, you can change the key by using the write-only Key property.
See Also
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.