5.3.1 Creating and Accessing Objects

You typically use the CreateObject method of the built-in Server object to create an instance of an object and assign it to a variable name. The following example creates an instance of the Browser Capabilities component and assigns it to the variable BrwsCap:
BrwsCap = Server.CreateObject("MSWC.BrowserType")
For more information on the Server.CreateObject method, see the section on the Server built-in object.
To create objects with application or session scope in the Global.asa file (see the section Global.asa later in this document), you use an extended form of the <OBJECT> tag. This tag must be placed outside of any <SCRIPT> tags. The object will not actually be created until it is used.
The objects declared in the Global.asa file are not created until the server processes a script that calls that object. This saves resources by creating only the objects that are needed.
The syntax of the extended <OBJECT> tag is:
<OBJECT RUNAT=Server ID=Identifier SCOPE=Scope
CLASSID="ClassID" PROGID="progID>
</OBJECT>
The following parameters are defined for the extended <OBJECT> tag:
- Scope
- Specifies the scope of the object. In the Global.asa file, Scope must be set to either Session or Application.
- Identifier
- Specifies a name for the object instance.
- ProgID
- An identifier associated with a class identifier. Either ProgID or ClassID must be specified in the <OBJECT> tag.
- ClassID
- Specifies a unique identifier for a COM class object. Either ProgID or ClassID must be specified in the <OBJECT> tag.
The first of the following examples creates an ADO Connection object with Session scope.
<OBJECT RUNAT=Server SCOPE=Session ID=conn
PROGID="ADODB.Connection">
</OBJECT>
The RUNAT=Server is required to distinguish the command from a client-side <OBJECT> tag. The Identifier is required for the object to be referenced by subsequent scripting code. Any objects declared in the Global.asa file can be used by any script in the application. For example, if you declared the following object:
---GLOBAL.ASA---
<OBJECT RUNAT=Server SCOPE=Session ID=MyConn
PROGID="ADODB.Connection">
</OBJECT>
You could reference the object MyConn from any page in the application:
---SOME.ASP---
<%= Set rs = MyConn.Execute("SELECT * FROM MyTable") %>
Copyright © 2000 Chili!Soft
|