Objects in session variables | | |
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 | | | | re: Objects in session variables
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.
"tshad" <tscheiderich@ftsolutions.com> wrote in message
news:%23Kf1ng2yFHA.2372@TK2MSFTNGP10.phx.gbl...[color=blue]
> 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
>[/color] | | | | re: Objects in session variables
"Peter Rilling" <peter@nospam.rilling.net> wrote in message
news:eS%23C2i2yFHA.428@TK2MSFTNGP15.phx.gbl...[color=blue]
> 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.[/color]
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[color=blue]
>
> "tshad" <tscheiderich@ftsolutions.com> wrote in message
> news:%23Kf1ng2yFHA.2372@TK2MSFTNGP10.phx.gbl...[color=green]
>> 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
>>[/color]
>
>[/color] | | | | re: Objects in session variables
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.
"tshad" <tscheiderich@ftsolutions.com> wrote in message
news:uDHhbq2yFHA.428@TK2MSFTNGP15.phx.gbl...[color=blue]
> "Peter Rilling" <peter@nospam.rilling.net> wrote in message
> news:eS%23C2i2yFHA.428@TK2MSFTNGP15.phx.gbl...[color=green]
>> 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.[/color]
>
> 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[color=green]
>>
>> "tshad" <tscheiderich@ftsolutions.com> wrote in message
>> news:%23Kf1ng2yFHA.2372@TK2MSFTNGP10.phx.gbl...[color=darkred]
>>> 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
>>>[/color]
>>
>>[/color]
>
>[/color] | | | | re: Objects in session variables
What happened when you tried? | | | | re: Objects in session variables
> Can you do something like - CType(Session("User"),User).firstName - or do[color=blue]
> you need to assign it first and then access the firstName?[/color]
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.
"tshad" <tscheiderich@ftsolutions.com> wrote in message
news:uDHhbq2yFHA.428@TK2MSFTNGP15.phx.gbl...[color=blue]
> "Peter Rilling" <peter@nospam.rilling.net> wrote in message
> news:eS%23C2i2yFHA.428@TK2MSFTNGP15.phx.gbl...[color=green]
>> 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.[/color]
>
> 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[color=green]
>>
>> "tshad" <tscheiderich@ftsolutions.com> wrote in message
>> news:%23Kf1ng2yFHA.2372@TK2MSFTNGP10.phx.gbl...[color=darkred]
>>> 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
>>>[/color]
>>
>>[/color]
>
>[/color] | | | | re: Objects in session variables
tshad wrote:[color=blue]
> 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
>
>[/color]
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.
--
Rob Schieber | | | | re: Objects in session variables
"Jason Kester" <jasonkester@gmail.com> wrote in message
news:1128711530.830902.243110@f14g2000cwb.googlegr oups.com...[color=blue]
> What happened when you tried?[/color]
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 |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,295 network members.
|