Progid = "scripting.dictionary">
Object>
Dictionary objects can also be used in the client's IE.
1. Summary of members of Dictionary objects
Tables 5-2 and Table 5-3 list the properties and methods of the Dictionary object and the corresponding description.
When a key / entry is added, if the key already exists; or when a key / entry is deleted, the keyword / entry does not exist, or change the COMPAREMODE of the Dictionary object that has the data containing the data, it will generate an error.
Table 5-2 Attributes and descriptions of Dictionary objects
Attribute
Comparemode (VBScript only for VBScript) setting or return string comparison mode
COUNT is read only. Returns the number of keys / entries in Dictionary
Item (key) setting or return the entry value of the specified key
Key setting key value
Table 5-3 Method and description of Dictionary object
Method
Add (Key, Item) Add key / entry to Dictionary
EXISTS (Key) If the specified key exists, return true, otherwise return false
Items () returns an array of all entries in the Dictionary object
Keys () Returns an array containing all keys in the Dictionary object
REMOVE (Key) Delete a specified key / entry
Removeall () Delete all keys / entries
2. Added and deleting entries for Dictionary
Once you get a new (empty) Dictionary, you can add an entry to get your entry and delete entries:
'In Vbscript:
Objmydata.add "mykey", "myitem" 'add value myitem with key mykey
Objmydata.add "Yourkey", "YourItem" 'add value YourItem with key yourkeyblnisthere = objmydata.exists ("mykey")' Returns True Because The item exists
Stritem = ObjmyData.Item ("Yourkey") 'Retrieve Value of YourKey
Stritem = objmydata.remove ("mykey") 'Retrieve and Remove Yourkey
Objmydata.removeall 'Remove All the item
In JScript, the equivalent code is:
// in jscript;
Objmydata.add ('mykey', 'myitem'); // add value myitem with key mykey
Objmydata.add ('Yourkey', 'YourItem'); // Add value YourItem with key yourkey
Var blnisthere = ObjmyData.exists ('mykey'); // returns true because the item exists
Var stritem = objmydata.item ('Yourkey'); // Retrieve Value of YourKey
Var stritem = objmydata.remove ('mykey'); // Retrieve and Remove Yourkey
Objmydata.removeall (); // Remove All the items
3. Modify the key or entries
You can change the data stored in Dictionary by modifying the value of the key or by modifying the entry associated with a particular key. The following code changes the data in the entries of the key to myKey.
Objmydata.item ("mykey") = "newValue" 'in VBScript
ObjmyData.item ('mykey') = 'newValue'; // in jscript
If the specified key is not found in Dictionary, create a Mykey as a key in Dictionary, a new key / entry to NEW VALUE for its entry value. Interestingly, if you use a non-existing key to retrieve entries, not only get an empty string (this is ideal), but also add a new key / entry pair in Dictionary, the key is the specified key But the data of the entry is empty.
You can use the key attribute only to change the value of the key without changing the data corresponding to the entry. Change an existing key mykey to MyNewkey, you can use:
Objmydata.key ("mykey") = "MyNewValue" 'in VBScript
Objmydata.item ('mykey') = 'mynewvalue'; // in jscript
If the specified key is not found, a running period error is generated.
4. Set comparison mode
Dictionary's CompareMode property is only available for VBScript and cannot be used in JScript. When comparing the string keys, the specified comparison is allowed. Two allowed values are binarycompare (0) and TextCompare (1). BinaryCompare (0) is a binary comparison (ie case sensitive); TextCompare (1) is a text control (ie, case sensitive). 5. Traverse Dictionary
When studying Dictionary, there are two ways and one attribute to take special attention, which allow us to traverse all keys / entries stored in Dictionary. The Items method returns all item data in Dictionary in the form of a one-dimensional array, and the Keys method returns all existing key values in a one-dimensional array. You can use the count property to get the number of keys or entries.
For example, you can use the following code to get all the keys and entries in the Dictionary named ObJMYDATA. Note that although the count property saves the number of key / entry in Dictionary, the array of VBScript and JScript is always starting from the subscript 0. Therefore, the array should be taken from 0 to count-1.
'In Vbscript:
Arrkeys = ObjmyData.keys' get all the keys into array
Arritems = ObjmyData.items' get all the items Into an array
For intloop = 0 to objmydata.count -1 'Iterate THROUGH THE ARRAY
StRTHISKEY = Arrkeys (Intloop) 'this is the key value
StRTHISITEM = Arritems (Intloop) 'this is the item (data) Value
NEXT
// in jscript
// Get VB-Style Arrays Using The Keys () and items () Methods
Var arrkeys = new vBarray (ObjmyData.keys ()). Toarray ();
Var arritems = new vbarray (ObjmyData.Items ()). TOARRAY ();
For (intloop = 0; intloop
// iprate through the arrays
StRTHISKEY = arrkeys [inTloop]; // this is the key value
StRTHISITEM = arritems [inTloop]; // this is the item (data) Value
}
In VBScript, you can also use the for Each ... next statement to complete the same function:
'Itereate The Dictionary as a Collection in Vbscript
For Each Objitem in Arritems
Response.write Objitem & "=" & Arritems (Objitem) & " "
NEXT
5.3.2 Dictionary Object Example
This book provides a range of sample files to test the various properties of the script running time library.
The default page of this chapter provides a series of VBScript example links available. Some examples are equally valid for JScript. These examples are stored in the corresponding subdirectory in the CHAPTER05 directory, and the displayed interface is shown in Figure 5-2: To view the runaryary object, click on the first link in the menu page, open the page named show_dictionary.asp. This page shows the content of the Dictionary object we provide, allowing you to test its properties and methods. The screen is shown in Figure 5-3:
1. Dictionary's global.asa file
One of the files provided with the Dictionary Object Sample page is Global.asa. It creates and pre-filled a Dictionary object of a session layer scope, so its content will not be lost between the page request. Generally speaking (considering scalability), this is not an ideal approach. In this example, you can see the effects of Dictionary's properties and methods.
If you download and install an example on your own server, you must create a virtual application based on this global.asa file. Or add its content to the Global.asa file in the root folder of the default site. In Chapter 3, how to create a virtual application with a wizard. However, for this example, the easiest way to create a virtual application is to right-click the Dictionary subfolder in the Chapter05 sample folder, in the Home Directory tab of the Properties dialog, click the Create button, as shown in Figure 5-4 :
In this global.asa file, the code uses the element to create a Scripting.Dictionary object instance of a session layer scope. Then put a series of values in Dictionary in the session_onstart event handler, and specify a reference to the Dictionary object to the ASP session variable myDictionary:
Progid = "scripting.dictionary">
Object>
SUB session_onstart ()
ObjbookList.Add "2610", "Professional Active Server Pages 3.0"
ObjbookList.Add "1274", "Instant JavaScript"
ObjbookList.Add "2882", "Beginning ASP Components"
ObjbookList.Add "1797", "Professional ASP Techniques"
ObjbookList.Add "1835", "AD0 2.0 Programmer's Reference"
Set session ("myDictionary" = objbookList
End Sub
Script>
2. Dictionary sample page
In the Scripting.Dictionary Object home page, the primary task is to get a reference to the Dictionary object instance of a session layer. Note that this reference is an object variable, so the set key must be used in VBScript. Then, check if you get an object (this is a good habit), if you don't have a virtual application that contains the Global.asa file correctly, check where the problem is. You will see our own message instead of an error message of the ASP (but note, the default error handling must be turned off for this).
<%
ON Error Resme next 'Turn Off Error Handling To Test IF Object EXISTS
'Retrieve Dictionary Object from User's Session
Set objmyData = session ("myDictionary")
IF Isobject (ObjmyData) THEN 'Found Dictionary Object In Session
...
%>
ITERATING THE DICTIONARY WITH ARRAYS DIV>
<%
ArrkeysArray = ObjmyData.keys' get all the key into an arch
Arritemsaray = ObjmyData.Items' get all the items into an arch
For intloop = 0 to objmydata.count - 1 'Iterate Through the array
Response.write "Key: " & ArrkeysArray (Intloop) & " B> Value: " _
& arritemsarray (intloop) & " b> "
NEXT
%>
...
... Other Code and Controls Go Here ...
...
<%
Else
'Could Not find Dictionary Object in the session
Response.write "Dictionary Object Not Available in Global.asa for Session"
END IF
%>
The Dictionary content list displayed on the page is the two arrays created using the KEYARY object of the Dictionary object, which can use the previous code to traverse them.
3. Dictionary page control
Under the list of Dictionary is a series of HTML controls, it can be used to set certain properties of the Dictionary object and perform various methods. These controls are all in one