| << 10.2.2- Creating an instance of a VBScript object | Chapter10 | 10.3.1- Don't use the Session Object with the Dictionary Object >> |
The Dictionary Object
When you pick up a copy of Webster's dictionary, you have the ability to find out information about a particular word. In this case, the information is the definition of the word. All of the words are organized in a particular order. In a dictionary, this order is alphabetical. Associated with every word in the dictionary is its definition. This could also very easily be a synonym for the word, or a picture of what the word represents. The key concept is that for each word, there is a piece of information.
The Dictionary object can be thought of as a Webster's for your application. You can store information in it and attach a keyword to each piece of information. Later, when you want to retrieve the information, all you do is provide the dictionary with the keyword, and it will return the information you have stored there.
Try It Out – Simple Dictionary Example
To start becoming familiar with the Dictionary object, let's take a look at a very simple example that shows you how to store and retrieve information from a Dictionary object.
1. Use your editor of choice to create the SimpleDictionary.asp file with the following source code.
<%
Dim objDictionary, strKey, strValue
Set objDictionary = CreateObject("Scripting.Dictionary")
objDictionary.Add "Apple", "Red"
objDictionary.Add "Lemon", "Yellow"
Response.Write "<P>All Data Stored in the Dictionary"
Response.Write "<P>Let's retrieve"
Response.Write "<HR>"
Response.Write "<P>Retrieving Data..."
strValue = objDictionary.Item("Apple")
Response.Write "<P>Value stored with key of 'Apple' is " & strValue
strKey = "Lemon"
strValue = objDictionary.Item(strKey)
Response.Write "<P>Value stored with key of '" & strKey & "' is " & strValue
%>
2. Save the file in your BegASPFiles folder.
3. View the page in your web browser.
|
|
How It Works
Using the Dictionary object is actually quite easy. The first step is to create an instance of the Dictionary object. The ProgID is Scripting.Dictionary, so all we need to do is:
Set objNewDictionaryObj = CreateObject("Scripting.Dictionary")
Once we have created an instance of the Dictionary object, we are ready to add data to it. The data that is stored in the dictionary is stored as a name/value pair. The name part of the pair is known as the key. When you add an item to the dictionary, you will use the Add method. This method takes the key and the value associated with the key and adds it to the Dictionary.
objDictionary.Add key, value
You can either pass the explicit values of the key and value parameters, or you can pass variables that hold the data that you want for either parameter.
After we have stored items in the Dictionary object, the next logical step is to retrieve the information. To access information in a Dictionary, you need to supply the key. Given that key, the Dictionary object will return the value associated with it. This returned value can either be stored in a variable, or used immediately in another method. As with the Add method, you can either pass an explicit reference to the key's name, or you can pass a variable that contains the name of the key.
Since in VBScript all variables are treated as variants, you don't need to worry about what the data type of the data that is stored in the Dictionary object is: except in one case, that is. If you have stored a reference to an object as a value in your dictionary, you need to use the Set statement to assign the reference to another variable.
To access the information for a specific key, you will use the Item method of the Dictionary object.
value = objDictionary.Item(key)
Now that we have loaded information into the Dictionary object and successfully retrieved it, the next step is to look at how to change the value of an entry in the Dictionary.
Try it Out – Changing Items in the Dictionary
1. Using your favorite ASP editor, enter the source code for ChangeValueDictionary.asp.
<%
Dim objDictionary, strKey, strValue
Set objDictionary = CreateObject("Scripting.Dictionary")
objDictionary.Add "Apple", "Red"
objDictionary.Add "Lemon", "Yellow"
strValue = objDictionary.Item("Apple")
Response.Write "<P>Value stored with key of 'Apple' is " & strValue
strKey = "Lemon"
strValue = objDictionary.Item(strKey)
Response.Write "<P>Value stored with key of '" & strKey & "' is " & strValue
Response.Write "<HR>"
objDictionary.Item("Apple") = "Green"
Response.Write "<P>Changed the value of Apple to Green"
Response.Write "<HR>"
Response.Write "<P>Value stored with key of 'Apple' is "
Response.Write objDictionary.Item("Apple")
Response.Write "<HR>"
objDictionary.Key("Lemon") = "Banana"
Response.Write "<P>Changed the key of Lemon to Banana"
Response.Write "<HR>"
Response.Write "<P>Value stored with key of 'Banana' is "
Response.Write objDictionary.Item("Banana")
%>
2. Save the file in your BegASPFiles folder.
3. View the ChangeValueDictionary.asp file in your web browser.
|
|
How It Works
In the first example, we manually stored information in the Dictionary object. In this example we do the same thing again. You might be tempted to think why not store this in a Session variable, and then this information would persist between pages? Unfortunately due to a fault in ASP this isn't possible, and if you try it, you will slow your server to a crawl. We'll point you to a workaround at the end of this section, but for now you'll have to accept that we need to populate the Dictionary object by hand, each time we use it.
The first part of the code is identical to our previous example:
Dim objDictionary, strKey, strValue
Set objDictionary = CreateObject("Scripting.Dictionary")
objDictionary.Add "Apple", "Red"
objDictionary.Add "Lemon", "Yellow"
We confirm once again that we've successfully stored our items and keys in the Dictionary object, by displaying them on the screen:
strValue = objDictionary.Item("Apple")
Response.Write "<P>Value stored with key of 'Apple' is " & strValue
strKey = "Lemon"
strValue = objDictionary.Item(strKey)
Response.Write "<P>Value stored with key of '" & strKey & "' is " & strValue
Response.Write "<HR>"
So far, so good, this is still the same as in the previous example. Next, we change the value contained within our Apple key from red to green. We display details showing that the information has been changed:
objDictionary.Item("Apple") = "Green"
Response.Write "<P>Changed the value of Apple to Green"
Response.Write "<HR>"
Response.Write "<P>Value stored with key of 'Apple' is "
Response.Write objDictionary.Item("Apple")
Response.Write "<HR>"
Our next step is to do the same for the second object we have stored in our dictionary, except this time we're changing the key, rather than the value. We change it in exactly the same way, except here we use the key method rather than the item method:
objDictionary.Key("Lemon") = "Banana"
Response.Write "<P>Changed the key of Lemon to Banana"
Response.Write "<HR>"
Response.Write "<P>Value stored with key of 'Banana' is "
Response.Write objDictionary.Item("Banana")
In the previous example, you saw that the Item method could be used to access the value. Now using this method, as you have seen above, it can also be used as the left side of an assignment statement to set the value for a particular key:
objDictionary.Item(keyValue) = newValue
We also changed the key itself. While this technically makes sense, from a real-world perspective it may not be a wise thing to do. If you are familiar with relational databases, then you know that each record in a table must have a unique key, and you cannot change the identity of that key. Changing the key itself in a Dictionary object is a very similar operation. A good word of advice is to be very sure you know why you are doing this when you decide to do it. The method we used to change the key itself was as follows:
objDictionary.Key (key) = newKey
These examples have shown you how to retrieve values from the Dictionary object by knowing the keys themselves. But what if you want to access information without knowing the keys? This next example will show you how to do this.
Try it Out – Retrieving Values When You Don't Know the Key
1. Using your favorite ASP editor, enter the source code for GetAllValuesDictionary.asp.
<%
Dim objDictionary, strKey, strValue
Set objDictionary = CreateObject("Scripting.Dictionary")
objDictionary.Add "Apple", "Red"
objDictionary.Add "Lemon", "Yellow"
strValue = objDictionary.Item("Apple")
Response.Write "<P>Value stored with key of 'Apple' is " & strValue
strKey = "Lemon"
strValue = objDictionary.Item(strKey)
Response.Write "<P>Value stored with key of '" & strKey & "' is " & strValue
Response.Write "<HR>"
Response.Write "<P>Retrieve list of keys"
For Each key in objDictionary
Response.Write "<P>Key = " & key & " -- Value = " & objDictionary.item(key)
Next
Response.Write "<HR>Retrieve list of values"
For Each item in objDictionary
Response.Write "<P>Value = " & objDictionary(item)
Next
%>
2. Save the file in your BegASPFiles folder.
3. View the GetAllValuesDictionary.asp file in your web browser.
|
|
How It Works
In this example, we are looking for a way to retrieve all of the keys or items in the Dictionary object at once. There are many practical applications of this. For example, you could have multiple pages all dumping information into a Dictionary object. Another page could quickly access all of the information without having to know all of the keys that have been added.
There are two sets of information that can be retrieved from the Dictionary object:
- A list of all keys in the Dictionary
- A list of all values for the keys in the Dictionary
There are different uses for both of these sets of information. If you retrieve all of the keys from a Dictionary object, then you can use the Item method to retrieve each corresponding value. Unfortunately, if you retrieve all of the items in the Dictionary object, there is no way to tie each item back to its key.
Once again we started by manually populating the Dictionary object. The first way we retrieved information in bulk from the Dictionary object was with the Keys method. This method returns an array, where each element contains one key. Once we have this array, we can loop through the array and display each of the keys on the client.
For Each key in objDictionary.keys
Response.Write "<P>Key = " & key & " -- Value = " & objDictionary.item(key)
Next
The other type of information that we retrieved in bulk from the Dictionary object was all of the values in the Dictionary. This was done using the Items method.
For Each item in objDictionary.items
Response.Write "<P>Value = " & item
Next
This method will return an array that contains all of the item values from the Dictionary object. With this array, we can iterate through it and display each of the values to the client. As we stated before, we have no way of retrieving the corresponding key for each item. So all we can do is display the value on the client.
Before we close this section on the Dictionary object, here's one word of warning.
| << 10.2.2- Creating an instance of a VBScript object | Chapter10 | 10.3.1- Don't use the Session Object with the Dictionary Object >> |

RSS




