473,387 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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

Jan 12 '07 #1
3 2806
Hi,

Benton wrote:
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
--
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
Jan 13 '07 #2
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?
On Jan 13, 1:45 pm, "Laurent Bugnion [MVP]" <galasoft...@bluewin.ch>
wrote:
Hi,

Benton wrote:
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,
-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 -
Jan 13 '07 #3
Hi,

Flinky Wisty Pomm wrote:
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
--
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
Jan 13 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
12
by: Steven T. Hatton | last post by:
This is something I've been looking at because it is central to a currently broken part of the KDevelop new application wizard. I'm not complaining about it being broken, It's a CVS images. ...
51
by: jacob navia | last post by:
I would like to add at the beginning of the C tutorial I am writing a short blurb about what "types" are. I came up with the following text. Please can you comment? Did I miss something? Is...
43
by: Mountain Bikn' Guy | last post by:
I have a situation where an app writes data of various types (primitives and objects) into a single dimensional array of objects. (This array eventually becomes a row in a data table, but that's...
7
by: Peter Morris | last post by:
What is the oposite of Object Oriented? C# is an object oriented language but C is a _______ language. -- _______________________ /_____________________(_) | _____________________ ...
0
by: Alexander Widera | last post by:
hello all, i have a problem ... like I already discussed in the thread "session empty" I have the following problem: I created a completely new web... i added 2 files: sessiontest1.aspx:
15
by: Edwin Knoppert | last post by:
I have searched but info is limitted. In my test app i used a non persistant cookie for forms authentication. slidingExpiration is set to true On run and close and rerun the login remains ok....
2
by: kito | last post by:
Hi, I wanted to get some advices from you guys. I have a little web-shop in ASP.NET for a project. I have several VB classes, where the most important ones are the "DBmanager" and the...
167
by: darren | last post by:
Hi I have to write a multi-threaded program. I decided to take an OO approach to it. I had the idea to wrap up all of the thread functions in a mix-in class called Threadable. Then when an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.