Microsoft KB Archive/165293

= How To Declare an Array at Application Level Scope in Active Server Pages =

Article ID: 165293

Article Last Modified on 5/2/2006

-

APPLIES TO


 * Microsoft Active Server Pages 4.0
 * Microsoft Internet Information Server 4.0
 * Microsoft Internet Information Services 5.0

-



This article was previously published under Q165293





IN THIS TASK
SUMMARY
 * Declaring Arrays at Application Level Scope

REFERENCES



SUMMARY
In developing a Web Application, you may want to declare a table of data for use by one or more pages at application level scope. This article demonstrates how to declare, populate, and reference an array that has been declared at application level scope.

back to the top

Declaring Arrays at Application Level Scope
Repeat the following steps for a demonstration on referencing arrays with application scope.  On a computer that is running Active Server Pages, create a folder named ATEST and configure Internet Information Server (IIS) to recognize this directory as a virtual root with execute permissions.  Create a file in this directory named Global.asa. Copy and paste the following code into this file:  SUB Application_OnStart ' This script executes when the first user comes to the site ' or the global.asa is modified.

' A simple fixed size array Dim aFixed(3) aFixed(1) = "Fixed" aFixed(2) = "Size" aFixed(3) = "Array"

' Cache the array as a member of the Application variables collection. Application("aFixed") = aFixed

' Declare a dynamic (resizable) array. Dim aColors

' Allocate storage for the array. Redim aColors(16)

' Store values representing a simple color table ' to each of the elements. aColors(1) = "RED" ' [#FF0000] aColors(2) = "GREEN" ' [#008000] aColors(3) = "BLUE" ' [#0000FF] aColors(4) = "AQUA" ' [#00FFFF] aColors(5) = "BLACK" ' [#000000] aColors(6) = "FUCHSIA" ' [#FF00FF] aColors(7) = "GRAY" ' [#808080] aColors(8) = "LIME" ' [#00FF00] aColors(9) = "MAROON" ' [#800000] aColors(10) = "NAVY" ' [#000080] aColors(11) = "OLIVE" ' [#808000] aColors(12) = "PURPLE" ' [#800080] aColors(13) = "SILVER" ' [#C0C0C0] aColors(14) = "TEAL" ' [#008080] aColors(15) = "YELLOW" ' [#FFFF00] aColors(16) = "WHITE" ' [#FFFFFF]

' Cache the array as a member of the Application variables collection. Application("aColors") = aColors

END SUB    Create a file in the same directory named Color.asp. Copy and paste the following code into the file: <%@ LANGUAGE="VBSCRIPT" %>   Arrays as Members of the Application variables collection. <%  if IsArray(Application("aColors")) then %>     A Resizable Array</H3> <%     ' Put a reference to the array in a temporary variable ' for easy access. aColors = Application("aColors") nColors = UBound(aColors) %>  <%   else Response.Write("Application('aColors') is not an array! <BR>") end if

if IsArray(Application("aFixed")) then %>     A Fixed Size Array</H3> <%     aFixed = Application("aFixed") for i = 1 to UBound(aFixed) Response.Write(aFixed(i) & "<BR>") next else Response.Write("Application('aFixed') is not an array! <BR>") end if   %>

</BODY> </HTML> </li> Save the files, launch a client browser such as Internet Explorer, and load the Active Server Page using a path similar to the following:

http:// /Atest/Color.asp

</li></ol>

The browser should display the contents of the arrays, which were stored at the application level.

Observe that, in the above sample, it is only during application startup that the contents of the variables collection of the Application object is altered. For that reason, there is no need to lock the Application object. As with any other element of the variables collection of the Application object, if the contents of the array were modified by another Active Server Page or via a session-level event, the Application object would need to be locked. For more information on the Lock and Unlock methods of the Application object, see the Active Server Pages Roadmap.

back to the top

<div class="references_section">