| Author |
Topic Search Topic Options
|
sabrin514
Newbie
Joined: 19 December 2003
Status: Offline
Points: 26
|
Post Options
Thanks(0)
Quote Reply
Topic: Problem with global variables Posted: 19 December 2003 at 8:41am |
I am having a problem. I have a web based application written using VB.NET where I have created a class object (person) to hold certain information. Since I used a wizard type style to collect all the necessary information, all pages in the project need to be able to see the information collected during the users session. For that reason, I defined the name of class object variable as a new instance of the class object in a code module in order to make it global.
The application works great, EXCEPT that if another user tries to use the application at the same time, the properties of that global class object change! I understand WHY this happens. The variable is global to the project and not to the session, but I cannot for the life of me figure out the proper way to go about it. The variables need to be global, but specific to EACH session.
So far I have read things about multithreading, session-wrappers, etc., but these seem fairly complex and I am not a code guru.
If anyone has any ideas, I would greatly appreciate some input on a stable and simple way to accomplish what I need to without having to take my application back to the drawing board.
Thank you.
|
 |
Mart
Senior Member
Joined: 30 November 2002
Status: Offline
Points: 2304
|
Post Options
Thanks(0)
Quote Reply
Posted: 19 December 2003 at 9:12am |
Try using Session variables, they are only for that user not the whole application e.g.:
'Set the session
Session("name") = name.Text
'Read the session
Dim name As String = Session("name")
If it is a very busy website session variables should not be used because they are stored on the server and can cause performance problems.
|
 |
sabrin514
Newbie
Joined: 19 December 2003
Status: Offline
Points: 26
|
Post Options
Thanks(0)
Quote Reply
Posted: 19 December 2003 at 9:22am |
Thank you for responding.
I know that session variables could work, but it could potentially become a busy site after implementation.
The other problem is that I have multiple instances of the class object person and using hard coded session variable names was what I was trying to get away from by using the class objects.
All of my subs and functions for reading, writing and updating the information stored in the class objects allows the name of the object to simply be passed in and the properties in the object contain all of the information that would need to be stored in separate session variables for each instance I am using.
I have read about collections and enumeration. Does anyone know if that might be a possible solution?
Thanks again.
|
 |
Mart
Senior Member
Joined: 30 November 2002
Status: Offline
Points: 2304
|
Post Options
Thanks(0)
Quote Reply
Posted: 19 December 2003 at 9:38am |
How about using a temporay XML file, write the details to a row in it and set a cookie corresponding to a row number. You can then load the XML file into a dataset and get the details from to row corresponding to the ID in there cookie.
Mart.
|
 |
MorningZ
Senior Member
Joined: 06 May 2002
Location: United States
Status: Offline
Points: 1793
|
Post Options
Thanks(0)
Quote Reply
Posted: 19 December 2003 at 9:39am |
I know that session variables could work, but it could potentially become a busy site after implementation.
Well.... if you are wanting to store some "global" variables on a user by user basis.. stashing something in Session/Cookies is the only way... application object WILL NOT be specific to a user.. and the line from your first post of: "The variables need to be global, but specific to EACH session.", makes it clear that you don't understand the diff between Application/Session
The other problem is that I have multiple instances of the class object person and using hard coded session variable names was what I was trying to get away from by using the class objects.
So what about all the code that uses whatever is stored? somewhere in there you need to be calling the "hard coded" variable names... i dunno, your design considerations make no sense....
I have read about collections and enumeration. Does anyone know if that might be a possible solution?
collection and enumerations aren't specific to "what" you are storing, so its not the total solution, you still need to store things in the Session realm to keep these set to a user by user basis
|
|
Contribute to the working anarchy we fondly call the Internet
|
 |
sabrin514
Newbie
Joined: 19 December 2003
Status: Offline
Points: 26
|
Post Options
Thanks(0)
Quote Reply
Posted: 19 December 2003 at 9:53am |
Well, sorry, didn't mean to make a confusing post. I guess I was just hoping that their was an easier way to do what I was trying to do.
It seems like .NET should have a built in scope that accomodates web applications, which they are most likely going to be multi-user and have similiar issues. My main problem with session variables is that as far as I know, they can't handle objects. Therefore, I'd need to have a session variable for every property in the object, and there are several instances of the object. This creates a nightmare. Maybe I am wrong about session variables or misunderstanding your recommendation, but I can't imagine that being the best way to handle problems of this nature.
Out of curiousity, how did web applications handle this before .NET saved us with the session variable? Since many users disable cookies, I am curious.
I think the way I will unfortunately will have to do this now is to create a collection, check to see if an instance of the class already exists, and if so use enumeration. I am worried this will create alot of overhead though, as I have been told that even when objects are explicitly disposed of after use, they are not always immediately discarded.
|
 |
sabrin514
Newbie
Joined: 19 December 2003
Status: Offline
Points: 26
|
Post Options
Thanks(0)
Quote Reply
Posted: 19 December 2003 at 10:02am |
Mart,
Your solution sounds interesting. I am not sure I am familiar enough with using XML to do something like that though. Is there alot entailed?
Thanks.
|
 |
MorningZ
Senior Member
Joined: 06 May 2002
Location: United States
Status: Offline
Points: 1793
|
Post Options
Thanks(0)
Quote Reply
Posted: 19 December 2003 at 10:21am |
It seems like .NET should have a built in scope that accomodates web applications
they did.. its called "Session" :)
and just for the record, file acess is way more CPU-age than memory access... an XML file would not be the best solution
|
|
Contribute to the working anarchy we fondly call the Internet
|
 |