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.
itemcan 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 = NothingProgramming 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
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access