HOWTO: Declaring an Array at Application Level ScopeID: Q165293
|
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.
Repeat the following steps for a demonstration on referencing arrays with
application scope.
<SCRIPT LANGUAGE=VBSCRIPT RUNAT=SERVER>
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
</SCRIPT>
<%@ LANGUAGE="VBSCRIPT" %>
<HTML>
<BODY>
<H2>Arrays as Members of the Application variables collection.</H2>
<%
if IsArray(Application("aColors")) then
%>
<H3>A Resizable Array</H3>
<%
' Put a reference to the array in a temporary variable
' for easy access
aColors = Application("aColors")
nColors = UBound(aColors)
%>
<TABLE BGCOLOR=BLACK>
<%
for i = 1 to nColors
cColor = aColors(i)
if cColor = "BLACK" then
cBGColor = "WHITE"
else
cBGColor = "BLACK"
end if
%>
<TR>
<TD BGCOLOR=<% =cBGColor %>>
<FONT COLOR=<% =cColor %>><% =cColor %></FONT>
</TR>
<% next %>
</TABLE>
<%
else
Response.Write("Application('aColors') is not an array! <BR>")
end if
if IsArray(Application("aFixed")) then
%>
<H3>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>
http://<server>/Atest/Color.asp
Active Server Pages Online documentation
For the latest Knowledge Base articles and other support information on
Visual InterDev and Active Server Pages, see the following page on the
Microsoft Technical Web Support site:
http://support.microsoft.com/support/vinterdev/
Additional query words:
Keywords : kbprg kbASP kbASPObj kbScript kbVisID kbGrpASP
Version : winnt:
Platform : winnt
Issue type : kbhowto
Last Reviewed: May 27, 1999