用application存数组的例子

发表于:2007-06-30来源:作者:点击数: 标签:
If you store an array in an Application object, you should not attempt to alter the elements of the stored array directly. For example, the following script does not work:br br lt;% Application(quot;StoredArrayquot;)(3) = quot;new valuequot
If you store an array in an Application object, you should not attempt to alter the elements of the stored array directly. For example, the following script does not work:<br>
<br>
&lt;% Application(&quot;StoredArray&quot;)(3) = &quot;new value&quot; %&gt;<br>
<br>
This is because the Application object is implemented as a collection. The array element StoredArray(3) does not receive the new value. Instead, the value would be included in the Application object collection, and would overwrite any information that had previously been stored at that location.<br>
<br>
It is strongly recommended that if you store an array in the Application object, you retrieve a copy of the array before retrieving or changing any of the elements of the array. When you are done with the array, you should store the array in the Application object again, so that any changes you made are saved. This is demonstrated in the following scripts.<br>
<br>
---file1.asp---<br>
&lt;%<br>
‘’Creating and initializing the array.<br>
dim MyArray()<br>
Redim MyArray(5)<br>
MyArray(0) = &quot;hello&quot;<br>
MyArray(1) = &quot;some other string&quot;<br>
<br>
‘’Storing the array in the Application object.<br>
Application.Lock<br>
Application(&quot;StoredArray&quot;) = MyArray<br>
Application.Unlock<br>
<br>
Server.Transfer(&quot;file2.asp&quot;)<br>
%&gt;<br>
<br>
---file2.asp---<br>
&lt;%<br>
‘’Retrieving the array from the Application Object<br>
‘’and modifying its second element.<br>
LocalArray = Application(&quot;StoredArray&quot;)<br>
LocalArray(1) = &quot; there&quot;<br>
<br>
‘’Printing out the string &quot;hello there.&quot;<br>
Response.Write(LocalArray(0)&LocalArray(1))<br>
<br>
‘’Re-storing the array in the Application object.<br>
‘’This overwrites the values in StoredArray with the new values.<br>
Application.Lock<br>
Application(&quot;StoredArray&quot;) = LocalArray<br>
Application.Unlock<br>
%&gt;<br>
<br>

原文转自:http://www.ltesting.net