HOWTO: Declaring an Array at Application Level Scope

ID: Q165293


The information in this article applies to:


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.


MORE INFORMATION

Repeat the following steps for a demonstration on referencing arrays with application scope.

  1. On a computer 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.


  2. Create a file in this directory named Global.asa. Copy and paste the following code into this file:
    
       <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> 


  3. Create a file in the same directory named Color.asp. Copy and paste the following code into the file:
    
       <%@ 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> 


  4. 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://<server>/Atest/Color.asp


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.


REFERENCES

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