Reputation: 59
I have a question about session variables and array assignments.
I have a two dimensional array with 10 rows and 20 columns. Then example 1 does not work and example 2 works:
Example 1:
session('anAr')(5, 10) = 'ab'
response.Write '<br>The new value is ' & session('anAr')(5, 10)
The new value of session('anAr')(5, 10) is printed as empty string.
Example 2:
dim localAr: localAr = session('anAr')
locarAr(0)(5, 10) = 'abc'
session('anAr') = localAr
response.Write '<br>The new value is ' & session('anAr')(5, 10)
Now the update to session ('anAr')(5, 10) has been done.
Although it works, I think the problem is that all the session('anAr')
is first copied to the localAr and then all the localAr is copied to the session('anAr')
.
Could an expert tell me please if there is any way to modify the session('anAr')(5, 10)
without copying of the session array to a local array?
Upvotes: 1
Views: 5540
Reputation: 16950
Unfortunetaly there's not. You have to use local arrays.
From Session Object (IIS)
If you store an array in a Session object, you should not attempt to alter the elements of the stored array directly. For example, the following script does not work:
<% Session("StoredArray")(3) = "new value" %>
The preceding script does not work because the Session object is implemented as a collection. The array element StoredArray(3) does not receive the new value. Instead, the value is indexed into the collection, overwriting any information stored at that location.
It is strongly recommended that if you store an array in the Session object, you retrieve a copy of the array before retrieving or changing any of the elements of the array. When you are finished using the array, you should store the array in the Session object again, so that any changes you made are saved, as demonstrated in the following example:
<%
'Creating and initializing the array
Dim MyArray()
Redim MyArray(5)
MyArray(0) = "hello"
MyArray(1) = "some other string"
'Storing the array in the Session object.
Session("StoredArray") = MyArray
Response.Redirect("file2.asp")
%>
Upvotes: 1