Sets a key in a Dictionary object
Methods
The Add Method
The Add method adds a key and item pair to a Dictionary object.
Syntax
object.Add key, item
Part Description
object Required. The name of a Dictionary Object
key Required. A key associated with the added item
item Required. A item associated with the added key
Example
dim d
set d=CreateObject("Scripting.Dictionary")
d.Add "n", "Norway"
d.Add "i", "Italy"
set d=nothing
The Exists Method
The Exists method checks whether a specified key exists in the
Dictionary object. It returns true if the key exists, and false
if not.
Syntax
object.Exists(key)
Part Description
object Required. The name of a Dictionary Object
key Required. The key value to search for
Example
dim d
set d=CreateObject("Scripting.Dictionary")
d.Add "n", "Norway"
d.Add "i", "Italy"
if d.Exists("n")= true then
Response.Write("Key exists.")
else
Response.Write("Key does not exist.")
end if
set d=nothing
The Items Method
The Items method returns an array of all the items in a
Dictionary object.
Syntax
object.Items
Part Description
object Required. The name of a Dictionary Object
Example
dim d
set d=CreateObject("Scripting.Dictionary")
d.Add "n", "Norway"
d.Add "i", "Italy"
Response.Write(d.Items)
set d=nothing
The Keys Method
The Keys method returns an array of all keys in a Dictionary
object.
Syntax
object.Keys
Part Description
object Required. The name of a Dictionary Object
Example
dim d
set d=CreateObject("Scripting.Dictionary")
d.Add "n", "Norway"
d.Add "i", "Italy"
Response.Write(d.Keys)
set d=nothing
The Remove Method
The Remove method removes a single key/item pair from a
Dictionary object.
Syntax
object.Remove(key)
Part Description
object Required. The name of a Dictionary Object
key Required. Key associated with the key/item pair you want
to remove
Example
dim d
set d=CreateObject("Scripting.Dictionary")
d.Add "n", "Norway"
d.Add "i", "Italy"
d.Remove("n")
Response.Write(d.Keys)
set d=nothing
The RemoveAll Method
The Remove method removes all the key/item pairs from a
Dictionary object.
Syntax
object.RemoveAll
Part Description
object Required. The name of a Dictionary Object
Example
dim d
set d=CreateObject("Scripting.Dictionary")
d.Add "n", "Norway"
d.Add "i", "Italy"
d.RemoveAll
Response.Write(d.Keys)
set d=nothing
Properties
The CompareMode Property
The CompareMode property sets and returns the comparison mode
for strings in a Dictionary object.
Syntax
object.CompareMode = compare
Part Description
object Required. The name of a Dictionary Object
=compare Optional. A value representing the comparison mode.
Has one of the following
settings: 0 = vbBinaryCompare performs a binary comparison 1 =
vbTextCompare
performs a textual comparison
Example
dim d
set d=CreateObject("Scripting.Dictionary")
d.CompareMode=1
d.Add "n", "Norway"
d.Add "i", "Italy"
d.Add "i", "Ireland" 'The Add method fails on this line because
the 'letter i already exists in the Dictionary
The Count Property
The Count property returns the number of key/item pairs in a
collection or Dictionary object.
Syntax
object.Count
Part Description
object Required. The name of a collection or Dictionary
object
Example
dim d, a, s, i
set d=CreateObject("Scripting.Dictionary")
d.Add "n", "Norway"
d.Add "i", "Italy"
Response.Write(d,Count)
set d=nothing
The Item Property
The Item property sets or returns the value of an item in a
collection or a Dictionary object.
Syntax
object.Item(key) = newitem
Part Description
object Required. The name of a collection or a Dictionary
Object
key Required. Key associated with the item
=newitem Optional. Used for Dictionary object only. Sets the
new value associated with
the specified key
Example
dim d
set d=CreateObject("Scripting.Dictionary")
d.Add "n", "Norway"
d.Add "i", "Italy"
Response.Write(d.item("n"))
set d=nothing
The Key Property
The Key property sets a key in a Dictionary object.
Syntax
object.Key(key) = newkey
Part Description
object Required. The name of a Dictionary Object
key Required. Key value that will be changed
=newkey Required. New value of the specified key
Example
dim d
set d=CreateObject("Scripting.Dictionary")
d.Add "n", "Norway"
d.Add "i", "Italy"
d.Key("i") = "it"
Response.Write(d.Item("it"))
set d=nothing
Examples
1. Exists
This example demonstrates how to first create a Dictionary
Object, and then use the Exists method to check if a specified
key exists.
Coding
<html>
<body>
<%
dim d
set d=CreateObject("Scripting.Dictionary")
d.Add "n", "Norway"
d.Add "i", "Italy"
if d.Exists("n")= true then
Response.Write("Key exists.")
else
Response.Write("Key does not exist.")
end if
set d=nothing
%>
</body>
</html>
Output
Key exists.
2. Items
This example demonstrates how to use the Items method to return
an array of all the items.
Coding
<html>
<body>
<%
dim d,a,i,s
set d=CreateObject("Scripting.Dictionary")
d.Add "n", "Norway"
d.Add "i", "Italy"
Response.Write("<p>The value of the items are:</p>")
a=d.Items
for i = 0 To d.Count -1
s = s & a(i) & "<br>"
next
Response.Write(s)
set d=nothing
%>
</body>
</html>
Output
The value of the items are:
Norway
Italy
3. Keys
This example demonstrates how to use the Keys method to return
an array of all the keys.
Coding
<html>
<body>
<%
dim d,a,i,s
set d=CreateObject("Scripting.Dictionary")
d.Add "n", "Norway"
d.Add "i", "Italy"
Response.Write("<p>The value of the keys are:</p>")
a=d.Keys
for i = 0 To d.Count -1
s = s & a(i) & "<br>"
next
Response.Write(s)
set d=nothing
%>
</body>
</html>
Output
The value of the keys are:
n
i
4. Item
This example demonstrates how to use the Item property to return
the value of an item.
Coding
<html>
<body>
<%
dim d
set d=CreateObject("Scripting.Dictionary")
d.Add "n", "Norway"
d.Add "i", "Italy"
Response.Write("The value of the item n is: " & d.item("n"))
set d=nothing
%>
</body>
</html>
Output
The value of the item n is: Norway
5. Key
This example demonstrates how to use the Key property to set a
key in a Dictionary object.
Coding
<html>
<body>
<%
dim d
set d=CreateObject("Scripting.Dictionary")
d.Add "n", "Norway"
d.Add "i", "Italy"
d.Key("i") = "it"
Response.Write("The key i has been set to it, and the value is:
" & d.Item("it"))
set d=nothing
%>
</body>
</html>
Output
The key i has been set to it, and the value is: Italy
6. Count
This example demonstrates how to use the Count property to
return the number of key/item pairs.
Coding
<html>
<body>
<%
dim d, a, s, i
set d=CreateObject("Scripting.Dictionary")
d.Add "n", "Norway"
d.Add "i", "Italy"
Response.Write("The number of key/item pairs are: " & d.Count)
set d=nothing
%>
</body>
</html>
Output
The number of key/item pairs are: 2