Objects in session variables

T

tshad

Can I directly access an object member from an object I have saved as a
session variable?

I am carrying a session variable for my user information as separate session
vaiables for each (firstName, lastName, email, userName etc).

I already have a User class and am thinking of saving the object in a
session variable.

Not sure what the pros and cons are here, but can I access the data directly
or do I need to assign it to a User object before I can access the members?

Thanks,

Tom
 
P

Peter Rilling

Either assign it to a User variable or cast the session version to User.
Either way, you have to let the runtime know what type it is so that it
knows what members are available.
 
T

tshad

Peter Rilling said:
Either assign it to a User variable or cast the session version to User.
Either way, you have to let the runtime know what type it is so that it
knows what members are available.

Can you do something like - CType(Session("User"),User).firstName - or do
you need to assign it first and then access the firstName?

Thanks,

Tom
 
P

Peter Rilling

I am not familiar with VB syntax, but you should be able to do that. Give
it a try and see what happens.

I know that in C# you can do ((User)Session["User"])).firstName.
 
K

Kevin Spencer

Can you do something like - CType(Session("User"),User).firstName - or do
you need to assign it first and then access the firstName?

Yes, but if you want more than one propety and/or method out of it, you'll
have to keep casting/converting it.

It is important to remember that it is a reference type. When you say,

Dim user As User

All you are doing is creating a variable. It has no size. It is null
(Nothing).

And when you say:

user = CType(Session("User"),User)

You have not duplicated the Session variable. You have simply referenced it.
Your variable is a pointer to a type of the class it is typed as. So, when
you assign it to an existing instance of a class, you are now pointing at
the same instance (the one in Session).

So, if you're worried about using too much memory, relax!

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 
R

Rob Schieber

tshad said:
Can I directly access an object member from an object I have saved as a
session variable?

I am carrying a session variable for my user information as separate session
vaiables for each (firstName, lastName, email, userName etc).

I already have a User class and am thinking of saving the object in a
session variable.

Not sure what the pros and cons are here, but can I access the data directly
or do I need to assign it to a User object before I can access the members?

Thanks,

Tom

Yes, you will need to mark the class as serializable though. Also, I
would be careful to this sparingly and only use primitive types as you
don't want your session to become bloated with objects.
 
T

tshad

Jason Kester said:
What happened when you tried?

Just finished looking at it.

What I found was that I didn't need to type it if I am looking at string
data from the object.

Sub Page_Load(sender as Object, e as EventArgs)
if not IsPostBack
Dim newUser = new User(152)
trace.warn("newUser.FirstName = " & newUser.FirstName)
Session("UserObject") = newUser
else
trace.warn("FirstName from Session " &
session("UserObject").FirstName)
end if
end sub

On the 1st page load it gets the User object fine and prints the first name
as expected.

As Rob mentioned, I had to recompile it adding serializable to the class.

On the 2nd page it went fine and printed the FirstName.

It must know that it is a User object because, just for grins, I gook out
the .FirstName from the trace:

trace.warn("FirstName from Session " & session("UserObject"))

And got the following error:

Cast from type 'User' to type 'String' is not valid.

So, you apparently don't need to cast it - if it is a string, anyway.

Thanks,

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top