Previous PageNext Page

5.3.2 Object Scope

The default scope for any object created with ASP is page scope. Any script in an ASP file can access any object created in that file. The object is created when the page is processed by the Chili!Soft ASP processor, and released after the page is delivered to the browser. Most objects should have page scope, but you can change the scope of an object to make it accessible from other ASP pages.
 

5.3.2.1 Page Scope

Any object created with the Server.CreateObject method on a page exists for the duration of that page. All scripts on the page can access the object, and the object is released when page processing is complete. The lifetime of an object is the lifetime of the page.
 
Because objects last during the processing of a page, objects should not be created within a loop. The following example create 1001 Connection objects:
 

<%

For I = 0 to 1000

Set Conn = Server.CreateObject("ADODB.Connection")

Conn.Open "connection string"

Next

%>

To conserve resources, create a single object, and then use the object's methods to open and close the resources of the object:
 

<%

Set Conn = Server.CreateObject("ADODB.Connection")

For I = 0 to 1000

Conn.Open "connection string"

Conn.Close

Next

%>

5.3.2.2 Session Scope

You can give an object session scope by storing the object in the Session object. Session scope should be used when the object is used by many scripts within a single user session. You can use either the <OBJECT> tag in a Global.asa file or the Server.CreateObject method to create an object with session scope.
 
In the Global.asa file, you can use the extended <OBJECT> tag with RUNAT attribute set to Server and the SCOPE attribute set to Session. The following example creates a session-scope instance of the Browser capabilities object:
 

<OBJECT RUNAT=Server SCOPE=Session ID=bc

PROGID="MSWC.BrowserType">

</OBJECT>

On an ASP page, you can also use the Server.CreateObject method to store an object in the Session built-in object. The following example stores an instance of the Browser capabilities object in the Session object.
 

<% Set Session("bc") = Server.CreateObject("MSWC.BrowserType") %>

5.3.2.3 Application Scope

Objects given application scope are created when the application starts and remain until the application ends. Application scope objects are shared by all user client sessions. You can use either the <OBJECT> tag in a Global.asa file or the Server.CreateObject method to create an object with session scope.
 
In the Global.asa file, you can use the extended <OBJECT> tag with RUNAT attribute set to Server and the SCOPE attribute set to Application. Once you have stored the object in the Application object, you can access the object from any page in the application.
 
On an ASP page, you can also use the Server.CreateObject method to store an object in the Application built-in object. For examples see the previous section.
 

Copyright © 2000 Chili!Soft

Previous PageTop Of PageNext Page