Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

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.

Posted

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.

Posted

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.

Posted
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...

A man and a dog have an average of three legs.

Beaware of Statistics.

Posted
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.

A man and a dog have an average of three legs.

Beaware of Statistics.

  • 7 months later...
Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...