The Dictionary Component

The Dictionary object of the Dictionary component is used for storage and is very much like an array. The Dictionary object has properties that make it more flexible—and in some cases much more usable—than an array. Unlike an array, a Dictionary object does not have to be declared to hold a certain number of elements; it is always dynamic. When you add an element to a Dictionary object, it grows to hold the element; when you remove an element, it shrinks. While you can do the same thing with a dynamic array, you have to manually manage the growing and shrinking of the array by using the Redim Preserve command.

A Dictionary object also lets you remove elements from any position within it. For instance, you can remove the first element and the Dictionary object will take care of freeing up that space. The element that was at position 2 is now at position 1, and so on.

The last significant advantage of using a Dictionary object is that, just like with a printed dictionary, you don't have to refer to the elements in the dictionary by a number; instead, you look them up using a key. When you add something to a dictionary, you not only add the actual value, you also add a string that will be used to look up the value later.

In the following statement, objDictionary is the variable that will hold a reference to the new Dictionary object. The methods and properties are described in Table 17-7 and Table 17-8.

 <OBJECT RUNAT=server PROGID=Scripting.Dictionary id=objDictionary> </OBJECT> 

Table 17-7. Methods in the Dictionary object.

Method Description
Add(Key, Item) Adds an item to the dictionary with the specified key. An item can be a variable containing any type of data—dates, strings, integers, or other objects.
Exists(key) Checks to see if the key exists in the dictionary. Returns True if the specified key exists in the Dictionary object, False if it does not.
Items Returns an array of all the items in the dictionary.
Keys Returns an array of all the keys in the dictionary. Each string in the resulting array is a key in the dictionary.
Remove(Key) Removes a key and item pair from the dictionary. The Dictionary object automatically frees up the space used by the key item pair.
RemoveAll Empties the dictionary. The Dictionary object frees up all the space used by key item pairs in the dictionary.

Table 17-8. Properties in the Dictionary object.

Property Description
CompareMode[= compare] Sets and returns the comparison mode for comparing string keys in a Dictionary object (See Table 17-9.)
Count Returns the number of items in the dictionary
Item(key)[ = newitem] Sets or returns an item for a specified key in a Dictionary object
Key(key) = newkey Sets or returns a key in a Dictionary object

Table 17-9 shows the possible values for the CompareMode property described earlier.

Table 17-9. The CompareMode property values.

Value Dictionary Lookup Type
0 Binary
1 Text
2 Database
>2 Values greater than 2 can be used to refer to comparisons using specific Locale IDs (LCID)

A Sample Dictionary Lookup

To illustrate the use of the Dictionary object, you can, for example, create a Web page that supplies information about some books. The code for this example is on the CD-ROM in the Components Web project. An HTML form collects the ISBN for a book that the user is interested in, and the response looks up that book in the dictionary and returns a string that describes the book. The code in Figure 17-9 shows the dictionary being loaded in the script itself, with only three books. In a production environment, you should probably load a dictionary from either a data file or a database. The HTML form simply prompts the user for the book he or she is interested in.

Figure 17-9. Source code for the Dictionary.htm page that captures an ISBN for a search using the Dictionary object.

 <HTML> <HEAD> <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> <TITLE>Dictionary Sample</TITLE> </HEAD> <BODY> <H2>Dictionary Sample</H2> <HR> This page searches the dictionary for book information <BR>based upon the ISBN. <P> Currently, the following three books are available: <UL> <LI>Inside Visual InterDev (1-57231-583-0) <LI>Running IIS 4.0 (2) <LI>Programming Visual InterDev 6.0 (3) </UL> <P> <FORM Action="DictionaryHandler.asp" METHOD=POST> Please enter an ISBN: <INPUT TYPE=TEXT NAME=ISBN> <P> <INPUT TYPE=SUBMIT> </FORM> <HR> </BODY> </HTML> 

When the user clicks the Submit button, data is passed to the DictionaryHandler.asp page, as shown in Figure 17-10. This page creates the dictionary and enters information about three books.

Figure 17-10. Source code for the DictionaryHandler.asp page that loads a dictionary with book information and performs a search given a key of the ISBN.

 <HTML> <HEAD> <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> <TITLE>Dictionary Sample</TITLE> </HEAD> <BODY> <H2>Dictionary Sample</H2> <HR> <OBJECT RUNAT=server PROGID=Scripting.Dictionary id=objDictionary> </OBJECT> <% ' Enter three books in the dictionary ' VI 1.0 objDictionary.Add "1-57231-583-0", "Inside Visual InterDev by " & _     "Miller, Spencer, Vincent, and Evans"       ' IIS 4.0 objDictionary.Add "2," "Running IIS 4.0" ' VI 6.0 objDictionary.Add "3," "Programming Visual InterDev 6.0 by " & _     "Miller, Spencer, and Evans" %> Your entry was ISBN <%=Request.Form("ISBN") %> <p> <% key = Request.Form("ISBN") If Not objDictionary.Exists(key) Then %> ISBN <%=key %> was not found in the dictionary! <% Else %> The book that corresponds with ISBN <%=Key %> is  <P> <B><I><%=objDictionary.Item(key) %></I></B> <% End If %> </BODY> </HTML> 

The first thing you do is create the Dictionary object and assign it to the objDictionary variable. Using the Add method, you add entries for the three different books into the dictionary. In production, you can use a TextStream object that contains the books and then load this object at the Session_OnStart event. This is a good approach, especially if the data will be used in more than one page and as long as there is not too much data for the Session object to carry around.

After loading the dictionary, the code checks that the user's input is in the dictionary with the Exists method. If it is False, it lets the user know that his or her entry was not found. If it does find it, it displays the information from the dictionary. Figure 17-11 shows the resulting output from the DictionaryHandler.asp page.

click to view at full size.

Figure 17-11. Output from the DictionaryHandler.asp page.



Programming Microsoft Visual InterDev 6. 0
Programming Microsoft Visual InterDev 6.0
ISBN: 1572318147
EAN: 2147483647
Year: 2005
Pages: 143

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net