473,586 Members | 2,792 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Nov 19 '05 #1
7 1604
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" <ts**********@f tsolutions.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
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

Nov 19 '05 #2
"Peter Rilling" <pe***@nospam.r illing.net> wrote in message
news:eS******** *******@TK2MSFT NGP15.phx.gbl.. .
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).fi rstName - or do
you need to assign it first and then access the firstName?

Thanks,

Tom
"tshad" <ts**********@f tsolutions.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
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


Nov 19 '05 #3
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" <ts**********@f tsolutions.com> wrote in message
news:uD******** *****@TK2MSFTNG P15.phx.gbl...
"Peter Rilling" <pe***@nospam.r illing.net> wrote in message
news:eS******** *******@TK2MSFT NGP15.phx.gbl.. .
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).fi rstName - or do
you need to assign it first and then access the firstName?

Thanks,

Tom

"tshad" <ts**********@f tsolutions.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
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



Nov 19 '05 #4
What happened when you tried?

Nov 19 '05 #5
> Can you do something like - CType(Session(" User"),User).fi rstName - 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.

"tshad" <ts**********@f tsolutions.com> wrote in message
news:uD******** *****@TK2MSFTNG P15.phx.gbl... "Peter Rilling" <pe***@nospam.r illing.net> wrote in message
news:eS******** *******@TK2MSFT NGP15.phx.gbl.. .
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).fi rstName - or do
you need to assign it first and then access the firstName?

Thanks,

Tom

"tshad" <ts**********@f tsolutions.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
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



Nov 19 '05 #6
tshad wrote:
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.

--
Rob Schieber
Nov 19 '05 #7
"Jason Kester" <ja*********@gm ail.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
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(sende r as Object, e as EventArgs)
if not IsPostBack
Dim newUser = new User(152)
trace.warn("new User.FirstName = " & newUser.FirstNa me)
Session("UserOb ject") = newUser
else
trace.warn("Fir stName from Session " &
session("UserOb ject").FirstNam e)
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("Fir stName from Session " & session("UserOb ject"))

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
Nov 19 '05 #8

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

Similar topics

2
2018
by: college | last post by:
I am trying to pass an array of user objects in session and take the array out of session and call the member functions of each object. However I am getting a Fatal error: Call to a member function on a non-object. I have session.autostart turned off, and I have the class definition for those objects included before the call to...
2
1566
by: June Moore | last post by:
Hi, I've got the following code that sets a session object. set obj = server.createobject("Scripting.Dictionary") set session("testobj") = obj If I want to remove the session object, do each of the following does the same job? Which one is the best? 1 --> set session("testobj") = null 2 --> set session("testobj") = nothing
14
2589
by: mjkahn | last post by:
I've read (and read!) that you shouldn't store objects in Session variables. I've read these reasons: - The object takes up memory that may not be freed until the session times out. Better to create the object only when you actually use it. - Causes poor performance because the thread that created the object has to service all requests...
10
1417
by: Roberto López | last post by:
Hi, I´m doing an asp.net application that uploads and downloads files and folders between the client and the server on my intranet. To do this I have create threads and it runs Ok but I need to show to the user the progress of the operation and here is the problem. I try to access to the Session Variables from inside the thread to show it on...
8
3166
by: Dave Wurtz | last post by:
All, I'm new to ASP development and I have a basic design question: Is it ok to store business objects to session variables or is there a better way to keep object information? For example, if a user logs onto the website, a user object is created that stores their full name, email address, street address, phone, etc. This object also...
4
1726
by: Andy Kasotia | last post by:
I have read couple of articles warning against the use of storing VB COM objects (Apartment Threading) in Session Variables due to the fact that these variables could go bad. My question is what's the workaround this? I have also read about making ASP Stateless...I'm guessing that means turning the session and application variables off...
4
1878
by: Not Me | last post by:
Hi, I have a set of pages that use forms authentication for access. There are times when one of the session objects I'm using appears to disappear. The session is not timing out, however. (if I go to a page that doesn't use that specific object it works fine and I'm not redirected to the login page) Apologies if I'm using the incorrect...
2
1436
by: HankD | last post by:
Hi, I am having a problem with instantiating two custom objects so they DO NOT point to the same memory location. What is happening is that changes I am making to my object1 are changing object2. I beleive this is because I set both to be equal to the same session variable. So when I change the value in test1.name it updates test2.name as well...
9
7285
by: david | last post by:
I have a class with some business-logic and with every roundtrip, I need an instance of this class, so I have to create it, every time again. That doesn't seem very efficient. I thought it would be 'better' to store an instance of this class in a session-variable, so it's available all the time and needs to be instanced only once. Is...
3
1963
by: Maximiliano | last post by:
Hello, I have an asp.net project that calculates a general tax. Ok, this tax is a big object formed with another child objects (as a mather of fact 15 another child object within it), like Ship, Vehicle, Bike, etc. each one could be null and has his own attributes and properties. For example Tax { int _period;
0
7908
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8336
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7950
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8212
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6606
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3835
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1447
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1175
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.