Scripting Objects
The vbScript library that provides the scripting language support
for AS Pals provides a number of other objects that can be used to
extend the developer's application. These are collectively known
as Scripting Objects. The three objects we will be concerned with
are:
Dictionary Object -- Allows the programmer to store information
in a single data structure similiar to a collection
FileSystemObject -- Provides utilities to locate, open, read and
close files on the server.
TextStream -- This objects allows you to manipulate the contents
of files.
Dictionary object
A dictionary object can be thought of a s a Webster's dictionary
for your application where you can store information in it, such
as descriptions and attach a keyword so that you can later locate
the information.
Creating Dictionary objects
We may create an instance of dictionary object like following:
'In VBScript
Dim objMyDictionary
objMyDictionary = Server.CreateObject("Scripting.Dictionary")
//In JScript:
var objMyDictionary = Server.CreateObject('Scripting.Dictionary');
Dictionary object methods
Add(key,item): Adds the key/item to the dictionary
Exists(key): Returns True if the specified key exists or False if
not.
Items(): Returns an array containing all the items in a Dictionary
object.
Keys(): Returns an array containing all the keys in a Dictionary
object.
Remove(key): Removes a single key/item pair specified by key.
RemoveAll): removes all the key/item pairs.
Dictionary Object properties:
CompareMode: (VBScript only). Sets or returns the string comparison
mode for the keys.
Count: Read only. Returns the number of key/tiem pairs in the Dictionary.
Item(key): Sets or returns the value of the item for the specified
key.
Key(key): Sets the value of key.
Adding and removing items
Once we create an instance of dictioanry, we can add items to it,
retrieve them and remove them
'In VBScript
objMyDictionary.Add "MyKey","MyItem" 'Add
value MyItem with key Mykey
objMyDictionary.Add "MyKey2","MyItem2" 'Add
value MyItem2 with key Mykey2
blnIsThere = objMyDictionary.Exists("MyKey") ' Return
true bacause the item exists;
strItem = objMyDictionary.Item("MyKey2") 'Retrieve
value of Mykey2
strItem= objMyDictionary.Remove("MyKEy") 'Retrieve
and remove MYkey
objMyData.removeAll ' Remove All the Items
//In JScript
objMyDictionary.Add 'MyKey','MyItem' //Add value MyItem with key
Mykey
objMyDictionary.Add 'MyKey2',"MyItem2' //Add value MyItem2
with key Mykey2
blnIsThere = objMyDictionary.Exists('MyKey') // Return true bacause
the item exists;
strItem = objMyDictionary.Item('MyKey2') //Retrieve value of Mykey2
strItem= objMyDictionary.Remove('MyKEy') //Retrieve and remove MYkey
objMyData.removeAll // Remove All the Items
Example 1 -- Save information about an Apple, Lemon and Cherry,
the color of each
<%
dim objDictionary
Set objDictionary=CreateObject("Scripting.Dictionary")
objDictionary.add "Apple","red"
objDictionary.add "Lemon","Yellow"
'Use hte Add method to add more items to the dictionary
'Syntax: object.add key, value
dim strKey
dim strValue
strKey="Cherry"
strValue="Red"
objDictionary.Add strKey,strValue
'Retrieve Data
Response.write " Retrieving Data..<br>"
strValue=objDictionary.Item("Apple")
Response.write "Value stored with the key of 'Apple'is " & strValue
'Use the Item method to locate items in the dictionary
strKey = "Lemon"
strValue = objDictionary.Item(strKey)
Response.Write "Value Stored with key of '" &strKey& "'
is " & strValue
Response.write "Store dictionary for later use<br>"
Set Session("MyDictionary") = objDictionary
Session("MyDictionaryStored") = True
Response.Write "Dictionary is stored in your session object."
%>
Example 2-- Changing information in the dictionary
<%
Dim bDictionaryExists
bDictionaryExists = Session ("MyDictionaryStored")
if bDictionaryExists <> true then
Response.Redirect "dic.asp"
else
dim objDictionary
Set objDictionary = Session("MyDictionary")
'Use the Exists property to determine if the Item exists. If so, change 'it's
value
if objDictionary.Exists("Apple")
then
objDictionary.Item("Apple") = "Green"
Response.write " Changed the value of Apple to Green<br>"
Response.write "Value stored with key of 'Apple' is " & objDictionary.Item("Apple") & "<br>"
end if
if objDictionary.Exists("Lemon")
then
objDictionary.Key("Lemon")= "Banana"
Response.write "Change the key of Lemon to banana"
Response.write "Value stored with key of 'Banana' is " & objdictionary.Item("Banana") & "<br>"
end if
end if
%>
|