5.2.5 reference object type library

Source: Internet
Author: User
Tags key string
5.2.5 reference object type library
In earlier ASP versions, when an object or component is used in a script, the public constants defined in the component (if any) are no longer valid in ASP. This means that we need to declare them (or their equivalents) and specify the corresponding values.
For example, when the ActiveX database object (ADO) component is used in earlier versions of ASP (detailed research will be conducted in Chapter 8th ), you have to add the predefined constant declaration to the open method of the record set. For example:
Const adopenkeyset = & h0001
Const adlockpessimistic = & h0003
Const adcmdtable = & h0002
...
Rs. Open "contact", "DSN = globalexampledata; uid = examples; Password = ;",_
Adopenkeyset, adlockpessimistic, adcmdtable
...
Another method is to use the # include command to insert a file named adovbs. inc into the page. This file is provided by IIS/asp and contains all the predefined constants required by ADO. Update Code You must confirm that the latest version is used and check that it is available for all page requests.
For IIS 5.0, there is a better way to add reference to the component or object type library by using the metadata command in the HTML annotation element (IIS 4.0 does not support this function ).
<! -- Metadata type = "typelib"
File = "path_and_name_of_file"
UUID = "type_library_uuid"
Verssion = "major_version_number.minor_version_number"
Lcid = "locale_id"
-->
Where:
? Path_and_name_of_file is the absolute physical path of a type of library file (. TLB) or ActiveX dll. You must provide this parameter or the type_library_uuid parameter.
? Type_library_uuid is the unique identifier of a database of this type. You must provide this parameter or the path_and_name_of_file parameter.
? Major_version_number.minor_version_number (optional) defines the version of the required component. If this version is not available, use the latest version.
? Locale_id (optional) is the region identifier. If no Type Library is found in this area, the computer uses the default (defined during installation) area.
Therefore, using this technology, you can use the following code to make the built-in ADO pre-defined constant available on the ASP page:
<! -- Metadata type = "typelib"
File = "C: Program filescommon filessystemadomsado15.dll"
-->
The file name msado15.dll can also be used for ADO components later than 2.50.
If ASP cannot load the type library, an error is returned and the execution of the page is stopped. Possible error messages are shown in Table 5-1:
Table 5-1 error code and description
False explanation
Description of invalid type libraries in ASP 0222
Type Library not found in ASP 0223
ASP 0224 Type Library cannot be loaded
ASP 0225 Type Library cannot be packaged (that is, ASP cannot create a Type Library packaging object from the specified Type Library)

5.2.6 create an object instance on the client
When discussing the technology of instantiating objects and components on the server in ASP, it is worth noting that the same work is done by running the client page in the browser. If you use ASP to create a script containing the client Program Or use the <Object> element to create a client component instance. In most cases, objects in the script runtime can be instantiated and used on the client, with the same effect as ASP on the server.
1. VBScript Createobject Method
The executable file of the extension file name.
Generally, the classid of an object is specified instead of the progid string, so that it cannot conflict with other objects installed on the client.
<Script language = "VBScript">
Dim objthis
Set objthis = Createobject ("CLSID: 892d6da7-e0f9-00105a42af30 ")
...
</SCRIPT>
Of course, you can also use progid and use common objects or components (especially the objects or components provided by standard installation), so the risk of component errors is very small:
<Script language = "VBScript">
Dim objthis
Set objthis = Createobject ("scripting. dictionary ")
...
</SCRIPT>
2. JScript activexobject Method
To instantiate JScript objects and components on the client, the activexobject method and the new operator must be used:
<Script language = "jscript">
VaR objmydata = new activexobject ('clsid: 892d6da7-e0f9-00105a42af30 ');
</SCRIPT>
Or:
<Script language = "jscript">
VaR objmydata = new activexobject ('this. object ');
</SCRIPT>
3. <Object> element technology
You can also use the <Object> element to create an instance of a client object or component. The runat attribute should be omitted or set to "client ". However, this attribute is ignored on the client. Therefore, the only purpose of setting this attribute is to prevent confusion when the <Object> element is used to instantiate the server component instance on the ASP page.
<Object ID = "objthis" runat = "client"
Classid = "CLSID: 892d6da7-e0f9-00105a42af30"
Codebase = "http://yourserver.com/components/mycomponent.cab”>
<Param name = "param1" value = "value1">
<Param name = "param2" value = "value2">
</Object>
Note: The codebase attribute shown here indicates that you can download and install components from URLs (if this component is not installed ). This feature is available in IE 3.0 or later versions.
You can view the website http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/object.asp,< Object> tags in the Windows 2000 Platform SDK documentation for the <Object> method used on the client, or read the book ie5 Dynamic HTML programmer 'reference, ISBN 1-861001-74-6, wrox press.

5.3 scripting. dictionary object
Many MicrosoftProgramming Language, Such as Visual Basic, VBScript, and JScript, all provide collections ). You can think of a set as an array. You can use built-in functions to complete basic tasks such as data storage and data manipulation. You don't have to worry about the columns in which data is stored, but use a unique key for access.
Both VBScript and JScript provide similar objects, collectively referred to as scripting. dictionary objects or dictionary objects. It is similar to a two-dimensional array that stores keys and related entries together. However, real object-oriented methods should not directly access data entries, but must be implemented using the methods and attributes supported by the dictionary object.
This chapter provides some sample pages that allow you to test the methods and attributes of objects during script runtime. These instances are in the chaper05 subdirectory of the downloaded file.

5.3.1 create and use a dictionary object
An example of creating a dictionary object is as follows:
'In VBScript:
Dim objmydata
Set objmydata = server. Createobject ("scripting. dictionary ")

// In JScript:
VaR objmydata = server. Createobject ('scripting. dictionary ');

<! -- Server-side with an object element -->
<Object runat = "server" Scope = "page" id = "objmydata"
Progid = "scripting. Dictionary">
</Object>
The dictionary object can also be used in IE of the client.
1. Summary of members of a dictionary object
Table 5-2 and Table 5-3 list the attributes and methods of the dictionary object and their corresponding descriptions.
When a key/entry pair is added, if the key already exists, or if a key/entry pair is deleted, the key/entry Pair does not exist, or changing the comparemode of a dictionary object that contains data will produce an error.
Table 5-2 attributes and descriptions of a dictionary object
Description of Ownership
Comparemode (for VBScript only) set or return key string comparison Mode
Count read-only. Returns the number of key/entry pairs in a dictionary.
Item (key) sets or returns the entry value of the specified key
Key (key) set the key value
Table 5-3 method and description of a dictionary object
Fang fa description
Add (Key, item) to add a key/entry pair to Dictionary
Exists (key) if the specified key exists, true is returned; otherwise, false is returned.
Items () returns an array containing all entries in the dictionary object
Keys () returns an array containing all keys in the dictionary object.
Remove (key) deletes a specified key/entry pair
Removeall () delete all key/entry Pairs
2. add and delete entries in dictionary
Once a new (empty) dictionary is obtained, you can add an entry to it to obtain the entry and delete the entry:
'In VBScript:
Objmydata. Add "mykey", "myitem" 'add value myitem with key mykey
Objmydata. Add "yourkey", "youritem" 'add value youritem with key yourkey
Blnisthere = 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 items
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 entry value
You can modify the data stored in a dictionary by modifying the key value or the data of the entries associated with the specified key. The following code changes the data in an entry whose key is mykey.
Objmydata. Item ("mykey") = "newvalue" 'In VBScript
Objmydata. Item ('mykey') = 'newvalue'; // In JScript
If the specified key is not found in dictionary, a new key/entry pair with mykey as the key and new value as the entry value will be created in dictionary. Interestingly, if you use a key that does not exist to retrieve an entry, you will not only get an empty string (this can be thought ), in addition, a new key/entry pair is added to the dictionary. The key is the specified key, but the data of the entry is empty.
You can use the key attribute to only change the key value without changing the corresponding data of the key. 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 runtime error occurs.
4. Set the comparison Mode
The comparemode attribute of dictionary is only applicable to VBScript and cannot be used in JScript. You can specify a comparison method when comparing string keys. The two allowed values are binarycompare (0) and textcompare (1 ). Binarycompare (0) is a binary comparison (case sensitive); textcompare (1) is a text comparison (that is, Case Insensitive ).
5. Traverse dictionary
When studying dictionary, there are two methods and one attribute that requires special attention. They allow us to traverse all key/entry pairs stored in dictionary. The items method returns all the entry data in the dictionary in the form of a one-dimensional array, while the keys method returns all existing key values in the one-dimensional array. You can use the Count attribute to obtain the number of keys or entries.
For example, you can use the following code to obtain all key and entry values in the dictionary named objmydata. Note: although the Count attribute stores the number of keys/entries in the dictionary, the arrays of VBScript and JScript always start with the subscript 0. Therefore, the array subscript should be from 0 to count-1.
'In VBScript:
Arrkeys = objmydata. Keys 'get all the keys into an 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 <objmydata. Count; intloop ++ ){
// Iterate through the arrays
Strthiskey = arrkeys [intloop]; // This is the key value
Strthisitem = arritems [intloop]; // This is the item (data) Value
}
You can also use for each… In VBScript... The next statement completes the same function:
'Iterate the dictionary as a collection in VBScript
For each objitem in arritems
Response. Write objitem & "=" & arritems (objitem) & "<br>"
Next

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.