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:
|
|||||||||||||
Dictionary Object Methods | |||||||||||||
The Dictionary object supports the following five methods:
|
Dictionary.Add Method | |
Syntax | |
dictionaryobject.Add key, item
|
|
Description | |
Adds a key and its associated item to the specified Dictionary object. |
|
Rules at a Glance | |
|
|
Example | |
The example uses a Dictionary object to store state abbreviations and their corresponding state names: Dim StateCode, StateName Dim StateDict Dim Key Set StateDict = CreateObject("Scripting.Dictionary") StateCode = "CA" StateName = "California" StateDict.Add StateCode, StateName StateCode = "NY" StateName = "New York" StateDict.Add StateCode, StateName StateCode = "MI" StateName = "Michigan" StateDict.Add StateCode, StateName Key = "NY" MsgBox StateDict.Item(Key) |
|
Programming Tips and Gotchas | |
|
|
See Also | |
Dictionary.Key Property |
|
Dictionary.CompareMode Property | |
Data Type | |
Long |
|
Description | |
Sets or returns the mode used to compare the keys in a Dictionary object. |
|
Rules at a Glance | |
|
|
Programming Tips and Gotchas | |
|
|
Dictionary.Count Property | |
Data Type | |
Long |
|
Description | |
A read-only property that returns the number of key/item pairs in a Dictionary object. |
|
Rules at a Glance | |
This property returns the actual number of items in the dictionary. So if you use the Count property to iterate the items in the dictionary, you would use code like the following: Dim ctr For ctr = 0 to dictionary.Count - 1 ' do something Next |
|
Dictionary.Exists Method | |
Syntax | |
dictionaryobject.Exists(key)
|
|
Return Value | |
Boolean |
|
Description | |
Determines whether a given key is present in a Dictionary object. |
|
Rules at a Glance | |
Returns True if the specified key exists in the Dictionary object; False if not. |
|
Programming Tips and Gotchas | |
|
|
Example | |
If oDict.Exists(strOldKey) Then oDict.Key(strOldKey) = strNewKey End If |
|
Dictionary.Item Property | |
Syntax | |
The syntax for setting an item is: dictionaryobject.Item(key) = item The syntax for returning an item is: value = dictionaryobject.Item(key)
|
|
Data Type | |
Any |
|
Description | |
Sets or returns the data item to be linked to a specified key in a Dictionary object. |
|
Rules at a Glance | |
|
|
Programming Tips and Gotchas | |
|
|
Example | |
The example uses the Dictionary object as a lookup table to retrieve the state name that corresponds to the state code entered by the user. The HTML page that submits user information to the server is as follows: <HTML> <HEAD><TITLE>Dictionary Object Example</TITLE></HEAD> <BODY> Enter your name and location: <P> <FORM METHOD=POST ACTION=dictobj.asp> Your name: <INPUT TYPE="Text" NAME="VisitorName" /><P> Your location: <INPUT TYPE="Text" NAME="City" />, <INPUT TYPE="Text" NAME="State" SIZE=2 /> <P> <INPUT TYPE="Submit" VALUE="Submit" /> </FORM> <BODY> </HTML> The ASP page that retrieves the information submitted by the user, encodes it, and uses the Dictionary object to retrieve the full state name is as follows: <HTML> <HEAD> <TITLE>ASP Page for the Dictionary Object Example</TITLE> </HEAD> <BODY> <% Show Greeting %> <SCRIPT LANGUAGE="VBScript" RUNAT="Server"> Sub ShowGreeting( ) Dim StateDict Dim ClientName, ClientState ' Initialize dictionary Set StateDict = Server.CreateObject("Scripting.Dictionary") StateDict.Add "NY", "New York" StateDict.Add "CA", "California" StateDict.Add "FL", "Florida" StateDict.Add "WA", "Washington" StateDict.Add "MI", "Michigan" StateDict.Add "MA", "Massachusetts" StateDict.Add "MN", "Minnesota" ' add other states ClientName = Server.HTMLEncode(Request.Form("VisitorName")) ClientState = Server.HTMLEncode(Request.Form("State")) Response.Write("Hello, " & ClientName & ". <P>") Response.Write("We are pleased to have a visitor from ") Response.Write(StateDict.Item(ClientState) & "!") End Sub </SCRIPT> </BODY> </HTML> |
|
Dictionary.Items Method | |
Syntax | |
dictionaryobject.Items
|
|
Return Value | |
A Variant array. |
|
Description | |
Returns an array containing all the items in the specified Dictionary object. |
|
Rules at a Glance | |
The returned array is always a zero-based variant array whose data type matches that of the items in the Dictionary object. |
|
Programming Tips and Gotchas | |
|
|
See Also | |
Dictionary.Keys Method |
|
Dictionary.Key Property | |
Syntax | |
dictionaryobject.Key(key) = newkey
|
|
Data Type | |
A String. |
|
Description | |
Replaces an existing key with a new one. |
|
Rules at a Glance | |
|
|
Example | |
Private Function ChangeKeyValue(sOldKey, sNewKey) 'Assumes oDictionary is a public object If oDictionary.Exists(sOldKey) Then oDictionary.Key(sOldKey) = sNewKey ChangeKeyValue = True Else ChangeKeyValue = False End If End Function |
|
Programming Tips and Gotchas | |
|
|
Dictionary.Keys Method | |
Syntax | |
dictionaryobject.Keys
|
|
Return Value | |
An array of strings. |
|
Description | |
Returns an array containing all the Key values in the specified Dictionary object. |
|
Rules at a Glance | |
The returned array is always a 0-based variant array whose data type is String. |
|
Programming Tips and Gotchas | |
The Keys method retrieves only the keys stored in a Dictionary object. You can retrieve all the Dictionary object's items by calling its Items method. You can recall an individual data item by using the Dictionary object's Item property. |
|
Example | |
Dim vArray vArray = DictObj.Keys For i = 0 to DictObj.Count -1 Response.Write vArray(i) & "<BR>" Next |
|
Dictionary.Remove Method | |
Syntax | |
dictionaryobject.Remove key
|
|
Description | |
Removes both the specified key and its associated data (i.e., its item) from the dictionary. |
|
Rules at a Glance | |
If key doesn't exist, runtime error 32811, "Element not found," occurs. |
|
Dictionary.RemoveAll Method | |
Syntax | |
dictionaryobject.RemoveAll
|
|
Description | |
Clears out the dictionary; in other words, removes all keys and their associated data from the dictionary. |
|
Programming Tips and Gotchas | |
If you want to remove a selected number of members rather than the entire contents of the dictionary, use the Remove method. |
|