Print Page | Close Window

Problem with global variables

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: ASP.NET Discussion
Forum Description: Discussion and chat on ASP.NET related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=8221
Printed Date: 28 March 2026 at 5:51am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Problem with global variables
Posted By: sabrin514
Subject: Problem with global variables
Date 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.




Replies:
Posted By: Mart
Date 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.



Posted By: sabrin514
Date 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.

 



Posted By: Mart
Date 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.



Posted By: MorningZ
Date 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


Posted By: sabrin514
Date 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.

 

 



Posted By: sabrin514
Date 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.

 



Posted By: MorningZ
Date 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


Posted By: sabrin514
Date Posted: 19 December 2003 at 10:25am

The session variables can't store objects or arrays though, right?

If they could I would be all set.



Posted By: MorningZ
Date Posted: 19 December 2003 at 10:33am

you can store anything you want in the Session

strings, numbers, datasets, objects, dates, anything you want



-------------
Contribute to the working anarchy we fondly call the Internet


Posted By: sabrin514
Date Posted: 19 December 2003 at 12:00pm

Thanks allot.

In that case, I think this will fix my problem.



Posted By: MorningZ
Date Posted: 19 December 2003 at 1:01pm

btw, how much info are you storing in this object?

theres some really cool stuff you can do with .NET's built in security IF the data isn't that much....

i use FormsAuthentication to store Roles, Username, UserID, TimeOffset, StateTheyLiveIn and something else which i forget right now......  and its near-mindless code to use once set up :)



-------------
Contribute to the working anarchy we fondly call the Internet


Posted By: sabrin514
Date Posted: 19 December 2003 at 1:15pm

I have 2 different class objects.

One class collects information about a hotline call. It has 16 properties. These properties are collected in pieces as the user progresses through the wizard. Some of the information cannot be obtained until the user is finished (example, call_end is the time the user completes the call).

The other other class is an Entity (essentially, a person), which has 20 properties. There can be up to 3 objects of this type per project depending on the choices that the user selects while stepping through the wizard.

The reason that I opted to go for the wizard type format was that the UI dynamically adapts depending on the options the user selects. I had previously only worked on applications of this nature  using JSP and Java. I think there is some different type of threading ability there because I don't remember any issues like this involving multiple users corrupting each other's data.

 



Posted By: Diep-Vriezer
Date Posted: 20 December 2003 at 5:40am
Have you tried the cache features of .NET? Those are really cool.

-------------
Gone..


Posted By: The WizeGuy
Date Posted: 20 December 2003 at 2:58pm

What kind of information do you need to save?

What "properties" do You have in your "person object"?

If You only need to store information like Username and if the user is Authenticated the HttpContext.User.Identity is Enough and is global

How do You get information to Your Personobject?

If it is with a login, what type of Login do You use? FormsAuthentication?

Lots of questions back ... but if we get answers to them we have a lot easier to "solve" your problem

cya,
/PatrikB



Posted By: MorningZ
Date Posted: 20 December 2003 at 4:16pm
read his last reply, he's storing a whole hell of a lot more information than just username

-------------
Contribute to the working anarchy we fondly call the Internet


Posted By: Diep-Vriezer
Date Posted: 21 December 2003 at 4:04am
Yeah, but can't you either cache it (using the SessionID) or just put it in the session?

-------------
Gone..


Posted By: Mart
Date Posted: 21 December 2003 at 4:50am

Yes you could use cache, heres an example:

Dim example(1) As String

example(0) = "Hello"
example(1) = "Hi"


'Add Item to cache
Cache.Insert("Example", Source, New CacheDependency(example))

'Read from cache
Dim example As String = Cache("Example")



Posted By: Diep-Vriezer
Date Posted: 21 December 2003 at 5:05am
Can you also DIM example as an array?

-------------
Gone..


Posted By: Mart
Date Posted: 21 December 2003 at 5:38am
Yes you can.


Posted By: sabrin514
Date Posted: 22 December 2003 at 8:32am

Thank you everyone for the responses. I didn't get a chance to read them over the weekend.

I have never used the cache feature, but it sounds pretty cool.  Is this information stored server side or client side, and are there any benefits to using cache as opposed to a session variable?

To answer the question about information being stored, the application is basically one that that is used by hotline staff who take calls/complainants. The one type of object (project), contains all the information about the complainant itself. There is only one of this type of object per case. The other type of object (entity) contains ionformation about a person or a company. There can actually be up to 3 of this these per call. The complainant, the caller (if its not the same as the complainant, and the target of the complaint.

 



Posted By: Diep-Vriezer
Date Posted: 22 December 2003 at 8:38am

Cache is server side ofcourse  I think cache might rescue you here. Google around abit and find some tutorials on it.



-------------
Gone..


Posted By: Mart
Date Posted: 22 December 2003 at 8:55am

Well obviously by its name cache is stored on the servers memory for quick access so it might be better to use cache. You can store anything in them just like in a session but there quicker. If you store loads of stuff in there (like a dataset containing a 6mb access database) and store it for every user on your site and you have lots of users your server wont be a happy bunny this christmas. But it will have the same effect with a session object. Anyhow point is dontn store lots of info in cache. Btw heres the microsoft tutorial about it.

http://www.dotnetjunkies.com/quickstart/aspplus/doc/datacaching.aspx - http://www.dotnetjunkies.com/quickstart/aspplus/doc/datacach ing.aspx



Posted By: sabrin514
Date Posted: 22 December 2003 at 9:17am

Thanks, I'll check out the tutorial on caching.

Storing my objects to a session variable worked like a charm. The only problem is that I can't seem to access the properties of my objects directly from the session variable and have to assign the objects stored in the session variable to objects each time I want to read or modify the properties.

Is there an easier way to do this? I feel like maybe I am missing something.



Posted By: Mart
Date Posted: 22 December 2003 at 9:21am

If you mean something like:

Dim ds As DataSet = Session("MyDataSet")

that is the only way to do it as far as i know.



Posted By: sabrin514
Date Posted: 22 December 2003 at 9:35am

Yep, that is what I meant.

Too bad there isn't a mechanism for accessing the properties directly. The session variables are useful, but in my case, since the information in the objects I need change in nearly every page as the user progresses through the wizard style input interface, it seems like a bit excessive.

How does garbage collection work with respect to session variables? Is there any way to destroy them immediately once they are no longer needed, or is this something that only the server can control?

 

 



Posted By: Mart
Date Posted: 22 December 2003 at 12:09pm

To unset a session just use

Session("key") = Nothing




Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net