Bruce
Bruce

Reputation: 2213

array session object

I am using session objects in C# ASP.net.

I like to know whether I can declare and use an array of such objects.

For example assume there is a session object "User", then I say:

User[,] u_sers_ = User[3,4].GetCurrentUser;

What I am trying to do is declare an array of session objects, is this possible?

If it is not possible, how do I declare an array of object "members"?

Thanks

Upvotes: 2

Views: 6109

Answers (1)

Andrew Barber
Andrew Barber

Reputation: 40160

Nothing in your code above involves any Session object. You may have some terminology confusion, here:

The object named Session is a member variable of the Page instance object, among others. It is there as a convenience to refer to the session object for the current user session. You do not do anything to create an 'array' of these objects - there is only one per-user-session currently active, and each Page and other similar object can only deal with the current one.

You put objects in the Session dictionary to use them. Again; this is a single object, automatically created and scoped per-session for you. You do not create it. But you do create individual objects to insert into the Session object, and it stores them for you, keeping track of which Session collection is attached to which session.

You could certainly put an array of something in the Session dictionary, but I don't think that's really what you are looking for here.


Edit Session usage:

In one page, you can add things to your Session object:

UserObject user = new UserObject(Request);
Session["user"] = user;
int[] integerValues = {1, 4, 6, 7, 44, 334, 3984};
Session["integers"] = integerValues;
object otherStuff = new object();
Session["other"] = otherStuff;

Then you can refer to those object by their name, after testing to make sure they really do exist and casting them properly. You always must test that the object exists, because the Session could expire at any time.

UserObject user = Session["user"] as UserObject;
if(user!=null)
    ...do stuff here...
int[] integerValues = Session["integers"] as int[];
if(integerValues!=null)
    ...do stuff here...
etc...

Upvotes: 6

Related Questions