Working with Cache and Viewstate

R

Ryan Ternier

I'm playing around with Cache and Viewstate.


I've made a small little example that reminds me of grade 12:

if(!IsPostBack)
{
ArrayList alTest = new ArrayList();
alTest.Add("This is from the ViewState");
alTest.Add("This is another from the viewstate");
alTest.Add("Wow, ViewState rocks for this");
ViewState["alTest"] = alTest;

alTest.Clear();

alTest.Add("This is from the CACHE!");
alTest.Add("EL CACHEO! CACHORAMA!");
alTest.Add("hi");
Cache["alTest"] = alTest;

}
else
{
foreach(string s in (ArrayList)ViewState["alTest"])
Response.Write("<BR>" + s);

Response.Write("<BR><BR>");

foreach(string sr in (ArrayList)Cache["alTest"])
Response.Write("<BR>" + sr);


}


When it posts back I get the results from the Cache, even when it reads
the Arraylist.


Now, i know this isn't because I used the name "alTest" for both the
ViewState and the Chache.

So I debugged through, and stopped when i hit the last line:
Cache["alTest"] = alTest;

When I looked... The ViewState and the Cache had the exact same values.

So I made a new Array for the Cache as follows:

alTestCache.Add("This is from the CACHE!");
alTestCache.Add("EL CACHEO! CACHORAMA!");
alTestCache.Add("hi");
Cache["alTest"] = alTestCache;

When I did this, I got the results I thought I would get....

I played more.... and it seems when: alTest.Clear is called
It modifies the viewstate that is bound to that object.

So when I clear it, it clears the ViewState.

Is there a way of stopping this? So you can save an object to the
ViewState, and then go and modify the contents of it, without it
affecting the view state (probably inneficient to do so, but I just want
know if there's a way).


/RT
 
D

David Lloyd

Ryan:

The ArrayList class inherits from System.Object with means it is a reference
type. When you set the value of the ViewState variable, I believe you are
passing a reference to the underlying ArrayList object rather than a copy of
the object. Therefore any actions performed on the underlying object will
affect the value in the ViewState.

All objects inheriting from System.Object inherit the MemberwiseClone method
which makes a "shallow" copy of the object (value types are copied, but
reference types just pass their reference). In order to have the ViewState
remain unchanged in your scenario, you would need to make a copy of the
object and assign it to the ViewState.

--
David Lloyd
MCSD .NET
http://LemingtonConsulting.com

This response is supplied "as is" without any representations or warranties.


I'm playing around with Cache and Viewstate.


I've made a small little example that reminds me of grade 12:

if(!IsPostBack)
{
ArrayList alTest = new ArrayList();
alTest.Add("This is from the ViewState");
alTest.Add("This is another from the viewstate");
alTest.Add("Wow, ViewState rocks for this");
ViewState["alTest"] = alTest;

alTest.Clear();

alTest.Add("This is from the CACHE!");
alTest.Add("EL CACHEO! CACHORAMA!");
alTest.Add("hi");
Cache["alTest"] = alTest;

}
else
{
foreach(string s in (ArrayList)ViewState["alTest"])
Response.Write("<BR>" + s);

Response.Write("<BR><BR>");

foreach(string sr in (ArrayList)Cache["alTest"])
Response.Write("<BR>" + sr);


}


When it posts back I get the results from the Cache, even when it reads
the Arraylist.


Now, i know this isn't because I used the name "alTest" for both the
ViewState and the Chache.

So I debugged through, and stopped when i hit the last line:
Cache["alTest"] = alTest;

When I looked... The ViewState and the Cache had the exact same values.

So I made a new Array for the Cache as follows:

alTestCache.Add("This is from the CACHE!");
alTestCache.Add("EL CACHEO! CACHORAMA!");
alTestCache.Add("hi");
Cache["alTest"] = alTestCache;

When I did this, I got the results I thought I would get....

I played more.... and it seems when: alTest.Clear is called
It modifies the viewstate that is bound to that object.

So when I clear it, it clears the ViewState.

Is there a way of stopping this? So you can save an object to the
ViewState, and then go and modify the contents of it, without it
affecting the view state (probably inneficient to do so, but I just want
know if there's a way).


/RT
 
R

Ryan Ternier

Hey David,

Thanks for the reply. That does make sense. I don't see a point in my
work where I would need to do that, but it was interesting to find out
it did that... and I wanted to know why.

Thanks :D

/RT
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top