Can array be declared in global.asa?

Results 1 to 2 of 2

Thread: Can array be declared in global.asa?

  1. #1
    Join Date
    Dec 1969

    Default Can array be declared in global.asa?

    How do I declare arrays globally?<BR><BR>Thanks.

  2. #2
    Join Date
    Dec 1969

    Default Yes and no.

    Yes and no.<BR><BR>You need to understand that the code in Global.asa is executed when a session begins or an application begins (or either ends)...and then that code "goes away"!!! <BR><BR>So if you want anything you do in code in Global.asa to "persist", then you have to store what you have done in some persistent manner.<BR><BR>And the way to do that is via Application or Session "variables" (badly misnamed! they are actually key/value pairs in a hashtable object...but never mind).<BR><BR>So if you wanted a *truly* global array, visible by all users, you&#039d put it into an Application Variable, perhaps thus:<BR><BR>Sub Application_onStart( )<BR> &nbsp; Dim counters( ) &#039 note: NO SIZE!<BR> &nbsp; ReDim counters( 100 ) &#039 *now* give it a size<BR> &nbsp; For i = 0 To UBound( counters )<BR> &nbsp; &nbsp; counters(i) = i &#039 this is silly, but just to demo concept<BR> &nbsp; Next<BR> &nbsp; Application("AppCounters") = counters &#039 name is of your choice<BR>End Sub<BR><BR>**************<BR><BR>Now, how do you *use* that array?<BR><BR>If you will *NEVER* modify that array once it is created, then you simply do this in your ASP pages:<BR><BR>&#060;%<BR>theCounters = Application("AppCounters")<BR>oneOfTheValues = theCounters( someElementNumber )<BR>%&#062;<BR><BR>Piece of cake. <BR><BR>But if you might modify an element of the array, or if you might change the size of the array (via ReDim Preserve), then you must be sure that two sessions don&#039t try to do it at the same time. Thus:<BR><BR>&#060;%<BR>Application.Lock &#039 lock out other sessions!<BR>counts = Application("AppCounters") &#039 get <BR>counts( 17 ) = counts( 17 ) + 1 &#039 modify<BR>Application("AppCounters") = counts &#039 store<BR>Application.Unlock &#039 allow other sessions in!<BR>%&#062;<BR><BR>You *MUST* try hard to lock the Application for the bare minimum needed time or you will create a horrible bottleneck in your system.<BR><BR>NOTE: Session variables do not need to involve locking; each session has its own set of session variables and there is never a conflict.<BR><BR>Hope this gets you started in the right direction.<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts