Microsoft Knowledge Base Email Alertz

(165293) - 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...

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 165293 - Last Review: May 2, 2006 - Revision: 6.1

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

This article was previously published under Q165293

On This Page

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.

Declaring Arrays at Application Level Scope

Repeat the following steps for a demonstration on referencing arrays with application scope.
  1. 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.
  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>
       <H3>Arrays as Members of the Application variables collection.</H3>
       <%
       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/search/default.aspx?qu=vinterdev (http://support.microsoft.com/search/default.aspx?qu=vinterdev)







APPLIES TO
  • Microsoft Active Server Pages 4.0
  • Microsoft Internet Information Server 4.0
  • Microsoft Internet Information Services 5.0
Keywords: 
kbaspobj kbcodesnippet kbhowto kbhowtomaster kbscript KB165293
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate