Friday 6 April 2012

ASP: the dictonary object

   


I've recently worked a lot on ASP arrays. One thing that is rarely used in ASP is the dictionary object which is similar to an array but has some useful methods and properties, making it easier to manage.

The dictionary object can store information in pairs: key and item. Every key refers to an item. When we build a dictionary object we create a relation between a key and an item, so we can recall an item by its key.

An example
To create a dictionary object we simply do the following:
<%
Dim dicObj
set dicObj = Server.CreateObject("Scripting.Dictionary")
Now our object is ready to accept keys and items. In order to add them we use the .add method:
dicObj.add "one","firstItem"
dicObj.add "two","secondItem"
and so on.
We can recall an item, by referring to its key:
response.write ("Item of key 'one' is " & dicObj.Item("one"))
%>
which will output:
Item of key 'one' is firstItem
Properties
The dictionary object has 4 properties.
CompareMode is used to compare objects in the dictonary object.
Count returns the number of items in the object.
Item returns the item.
Key sets a new key value for an already existing key.

Methods
Let's now see the methods for the dictionary object.
Add adds a new pair (as seen in the example).
Exists returns a boolean value indicating if a key exists in the object.
Items returns the array of items.
Keys returns the array of keys.
Remove removes a key/item pair from the object.
RemoveAll removes all the pairs from the object.

I hope you've found the post interesting.

0 thoughts:

Post a Comment

Comments are moderated. I apologize if I don't publish comments immediately.

However, I do answer to all the comments.