Billy
Billy

Reputation:

asp.net - C# Session variable being modified by reference?

It looks like the value in my session object changes when I modify a local variable set from session.

Setup:

.net 2.0

BasePage retrieves a complex object from session , casts it, and stores it in a class level variable. Child Page uses class level variable to do play out some optional scenarios. User decides to not keep changes, session object is not updated from variable, however when you retrieve the value from session it contains those unsaved changes.

In debug mode looking at the the session object, it changes at the same time the local object changes.

Thanks!

Upvotes: 1

Views: 2928

Answers (1)

tvanfosson
tvanfosson

Reputation: 532435

You're returning a reference to the object in the session so this is how it should work. If you want to work with a different object, you should make a clone of the object in the session to work with, then replace the one stored in the session with the clone if the user decides to accept the changes.

Upvotes: 2

Related Questions