OnTheAnvil Posted March 11, 2004 Posted March 11, 2004 I started a failry large project 4 months ago and I needed to pass variables between forms. I asked a guy I work with what he thought the simplest way to do this was. He recommended creating a new Gbl.vb class file and then inside it placing code like this... Module Gbl Public gUserName As String Public gSecurityLevel as String End Module This way all I have to do is from anywhere in my code, either a class or a form, I can make statements like this txtBox1.Text = gUserName The only problem is that now after 4 months of coding and using this module to hold about 30 different global variables I've found a huge problem. When one user logs on as USER1 their username is displayed at the top of the page as they conitnue to work like it should be. However, when USER2 logs in the global variable changes for both people and both people's browsers report that they are logged in as USER2. Huge problem! This means that if 50 people log in everyone has the username and security permissions of the last person who logged in. I was told by the same person who recommened that I use this that they had tested it before and it worked for them. They suggested maybe it was an IIS setting. Anyway I know I should have used session variables now but converting to that is going to take a huge amount of time. Is there anyway I can force this module to make those Public variables session dependent so each new session keeps track of their own variables instead of everyone sharing one set? If not could you please offer a suggestion on how to change my code fairly quickly. I'm in a time crunch right now. Thanks, OnTheAnvil Quote
kahlua001 Posted March 11, 2004 Posted March 11, 2004 HOw about using a session variable instead. Quote
Administrators PlausiblyDamp Posted March 11, 2004 Administrators Posted March 11, 2004 Rather than storing them in a module (bad OO practice anyway) you could store them in a class and then store the class in a session variable. May not be the cleanest code but will probably be the quickest fix if time is short.... Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
OnTheAnvil Posted March 11, 2004 Author Posted March 11, 2004 Thanks PlausiblyDamp, Could you give a quick example of that? I know a tiny bit about session variables and storing the class in a session sounds a little confusing. My biggest problem is that inside that global module I have functions and subroutines that use those variables. And to my knowledge you can't view session variables inside a module. If I'm wrong please correct me. Thanks for your response. I'm starting to see some light at the end of the tunnel. Quote
OnTheAnvil Posted March 11, 2004 Author Posted March 11, 2004 Still need help I've been working more with my code this morning and I think if someone can tell me how to access the session variables from within the module I'll be able to take a few hours running through a search and replace and fix my problem. So far though it seems like accessing the session object in a global class or module, i.e. Gbl.vb, doesn't work. Any thoughts would be great. Quote
OnTheAnvil Posted March 11, 2004 Author Posted March 11, 2004 Solution Here is the current solution I found in case anyone else is an idiot like me and tries to use a global module. Inside your global module you can access the CURRENT session's variables by doing this... System.Web.HttpContext.Current.Session("UserName") On any standard webform you can of course use the regular form like this... Session("Username") Now all that I have to do is a careful search and replace of all my global variables and convert them into session variables and it SHOULD work. Thanks for the replies guys. You got me steered in the right direction. Quote
hrabia Posted March 11, 2004 Posted March 11, 2004 BTW: the VB compiler translates a module into class in which all members are marked as Shared (static in c#). The genereted class is NotInheritable (sealed). A static field denotes exactly ONE storage location. No matter how many instances of a class are created, there is only ever ONE copy of static field... Quote A man and a dog have an average of three legs. Beaware of Statistics.
OnTheAnvil Posted March 11, 2004 Author Posted March 11, 2004 Thanks, Is there anyway to convert the module into a Class where the variables have exactly one storage location for each session? Quote
hrabia Posted March 11, 2004 Posted March 11, 2004 Thanks, Is there anyway to convert the module into a Class where the variables have exactly one storage location for each session? Don't mix two things: static type members ("language level") and type of state (ASP.Net). Your problem is to share objects between pages and between sessions. To share objects between pages use session state (that's the simplest way) and to share objects across all clients use application state. Quote A man and a dog have an average of three legs. Beaware of Statistics.
OnTheAnvil Posted March 11, 2004 Author Posted March 11, 2004 Alright, thanks. I did a search and found a good knowledge base article on the difference. Quote
joe_pool_is Posted October 24, 2004 Posted October 24, 2004 Alright' date=' thanks. I did a search and found a good knowledge base article on the difference.[/quote']Where? I'd like to read that article, if you don't mind sharing. Quote Avoid Sears Home Improvement
OnTheAnvil Posted October 24, 2004 Author Posted October 24, 2004 Unfortunately I didn't bookmark the article but if you search in you .NET help with the keywords "session application state" you should find an article called: "State Management Recommendations". I hope that helps. ~OtA Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.