Last post Jan 18, 2011 07:41 PM by mrainsdon
Jan 17, 2011 01:57 PM|mrainsdon|LINK
I have the following issue:
SomeObject myObject = new SomeObject();
// Save myObject to session variable
Session["OriginalObject"] = myObject;
// Save it to a different Session variable
Session["CurrentObject"] = myObject;
myObject.string1 = "This is a test";
SomeObject myOriginalObject = (SomeObject)Session["OriginalObject"];
// when I check the value of myOriginalObject.string1 it is set to "This is a test"
// how does this happen? The object myOriginalObject gets instantiated from the Session variable after myObject.string1 is set to a value.
What am I missing?
The problem exists when the session state mode is in its default state. When I change the mode to StateServer then it works the way I would expect it to work.
It seems strange that the 2 modes would cause the Session variable to work differently.
Jan 17, 2011 03:33 PM|SGWellens|LINK
The object myOriginalObject gets instantiated from the Session variable
No. You've only instantiated it once and there is only one copy.
You can put the same item in ten different collections and it will still only exist once.
Jan 17, 2011 06:06 PM|mrainsdon|LINK
I understand that I only instantiated it once and that setting it to another variable name only creates another variable that points to the same object.
But the concept of a Session variable persisting for the entire session no matter what pages come and go, it did not make sence that there was only one object in memory.
When I change the Session State mode from its default to StateServer then it works the way I would expect it to work, with each session variable containing its own copy of the object.
Jan 17, 2011 08:08 PM|SGWellens|LINK
You shouldn't go back to your ealier posts and edit them to change their meaning. It makes the threads confusing and difficult to follow.
Jan 18, 2011 06:41 AM|jitendra_faye|LINK
Actually u have created Memory for "myObject" Object once. So if u will any changes on that object then it will automatically reflect to
other object (in Session variable), where u are storing this. If u want to to get different copy then create another object and then make changes on that object then
after assign to Session Variable then only u will get different Value.
Jan 18, 2011 07:41 PM|mrainsdon|LINK
I edited the earlier post before I knew you had replied to it. I will try to not make the same mistake again.