2

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

1 Answer 1

6

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...
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, let's say I have the following as a session member: public string Trip_Name { get { return _Trip_Name; } set { _Trip_Name = value; } } How do I make this as an array?
You don't add members to Session; you add objects to its Dictionary. I'll demonstrate with an edit to my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.