What's better for Session[]... object or struct?

  • Thread starter Thread starter Benton
  • Start date Start date
B

Benton

Hi there,

Currently I have a rather simple class with a few members:

string userName;
string password;
string centers;
bool canModify;

I have correspondant GET properties for them in the class. I need to store
an instance in the Session, like this:

Session["up"] = new UserProperties("joe", "1234", "a,b,c", true);

Is it better to use a class like this or will I be better with a struct?

Regards,

-Benton
 
Hi,
Hi there,

Currently I have a rather simple class with a few members:

string userName;
string password;
string centers;
bool canModify;

I have correspondant GET properties for them in the class. I need to
store an instance in the Session, like this:

Session["up"] = new UserProperties("joe", "1234", "a,b,c", true);

Is it better to use a class like this or will I be better with a struct?

Regards,

-Benton

Both are possible, but I prefer classes, because classes are "by
reference" whereby struct are "by value". The consequence of it is that,
when you get an existing struct from the Session and modify it, you have
to "re-save" it in the Session object, or else the changes are not
saved. This is not the case with classes.

HTH,
Laurent
 
I didn't know that...

So
Foo myFoo = Session["someFoo"];
myFoo.BarString = "Rhubarb";

actually modifies the instance of Foo stored in session state? Or do
you just mean that

Session["myFoo"].BarString = "Rhubarb"; will only persist for reference
types?


Hi,




Hi there,
Currently I have a rather simple class with a few members:
string userName;
string password;
string centers;
bool canModify;
I have correspondant GET properties for them in the class. I need to
store an instance in the Session, like this:
Session["up"] = new UserProperties("joe", "1234", "a,b,c", true);
Is it better to use a class like this or will I be better with a struct?

-BentonBoth are possible, but I prefer classes, because classes are "by
reference" whereby struct are "by value". The consequence of it is that,
when you get an existing struct from the Session and modify it, you have
to "re-save" it in the Session object, or else the changes are not
saved. This is not the case with classes.

HTH,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering:http://www.galasoft-LB.ch
PhotoAlbum:http://www.galasoft-LB.ch/pictures
Support children in Calcutta:http://www.calcutta-espoir.ch- Hide quoted text -- Show quoted text -
 
Hi,
I didn't know that...

So
Foo myFoo = Session["someFoo"];
myFoo.BarString = "Rhubarb";

actually modifies the instance of Foo stored in session state?

If Foo is a class:

The "myFoo" variable will contain a reference to the instance. The
Session object also contains a reference to the same instance. The
instance itself is not contained in the Session state, just the
reference to it.

However, if Foo is a struct:

The "myFoo" variable will contain a copy of the instance contained in
the Session object (because struct are by value).

Or do
you just mean that

Session["myFoo"].BarString = "Rhubarb"; will only persist for reference
types?

No, for value types too, just the way they're stored is different.

Here's an example:

protected void Page_Load( object sender, EventArgs e )
{
if ( Session[ "Class" ] == null )
{
Session[ "Class" ] = new TestClass( "Hello" );
}
else
{
TestClass myClass = (TestClass) Session[ "Class" ];
myClass.myString = "World";
}

if ( Session[ "Struct" ] == null )
{
TestStruct myStruct = new TestStruct();
myStruct.myString = "Hello";

Session[ "Struct" ] = myStruct;
}
else
{
TestStruct myStruct = (TestStruct) Session[ "Struct" ];
myStruct.myString = "World";
}

// Displays "World"
lblClass.Text
= ( (TestClass) Session[ "Class" ] ).myString;

// Displays "Hello"
lblStruct.Text
= ( (TestStruct) Session[ "Struct" ] ).myString;
}


Greetings,
Laurent
 
Back
Top