Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ok, so I have a page that in vb.net I needed a global variable (AllTeams) as I need this variable to be used throughout the entire codebehind.vb page functions.

There is a functionality that allows a user (all values are stored in a class, no database) to update the values of a class, but when the page is reloaded, the global variable runs again and it basically starts as if no update had occurred. I tried to put an if-then statement on the global variable, but apparently I cannot do so outside of modules/functions

How can I do this? Any help will be greatly appreciate.

Thank you!
Posted

There are no such thing as "global variable" in .NET. There is nothing good about "global variables", so it's only good that .NET came out without this trash. Besides, you should understand that the ASP.NET application lifetime (in terms of the code behind of a page) all lies between HTTP request and HTTP response it generates. And HTTP protocol is stateless:
http://en.wikipedia.org/wiki/HTTP[^],
http://en.wikipedia.org/wiki/Stateless_protocol[^].

That said, to communicate between pages, you need something to store the state anyway. For example, it could be the state stored in a database. The server-based approach is the session state management. Please start here:
http://msdn.microsoft.com/en-us/library/ms178581.aspx[^].

—SA
 
Share this answer
 
Comments
TheAbominable 16-Oct-13 21:58pm    
Sergey, thank you for the response, but I DO NOT have a database. Basically, I'm displaying values of a class on a website (which is why I have a 'global variable' in the default.aspx.vb / codebehind) which are stored in a variable used by all the functions in the codebehind. Given that you've mentioned that using a global variable is not a good practice, I'll look at the links you've posted, however, and I repeat, I'm NOT USING a database. Thank you and any further help will be highly appreciated.
Sergey Alexandrovich Kryukov 16-Oct-13 22:18pm    
Who told you that you should use the database? Read again. I repeat, read again, this time, more thoroughly.
Whatever you do, you need the storage for the state, period. Read about next (and main) option: session state, last link.
—SA
To expand a bit on what Sergey said, there are several ways you can temporarily store data between page loads. Unfortunately, all of them have their drawbacks.

Probably the easiest is to save your data in the Session object. Any object can be stored. Classes and other complex objects should be tagged with the Serializable attribute. The downside is that Session has a tendency to reset, especially if you are storing large quantities: it is fine for holding data from one page while the next one loads, but it is not reliable enough to be considered "global."

The next option is cookies. This really isn't a very good option, in my opinion: the size of a given cookie is limited to 4k, which may not be big enough to hold your data, and browsers can be set to ignore cookies, which makes this very undependable.

You could compress the data into an ASCII format and pass it as part of the URL. This is done on some web systems to support cookieless browsers, but it has a lot of limitations as well as potentially fatal security flaws (you probably do not want someone bookmarking a URL with encrypted data.) Avoid this entirely.

The fourth option, which is what I will often use, is to use the Profile structure. There is a bit of a learning curve, but this essentially links your web app with a SQL database, with infrastructure managed by the Framework. Once it is in place, it is easy to use. It is also much more flexible, as any type or quantity of data can be stored and the data is guaranteed to persist until the user times out.

You could also try to roll your own Profile alternative, with you manually creating data stores (XML structures, database entries, comma delimited text files, whatever) as needed to hold your information. The problem is making sure that each web user has an ID that will remain consistent over time -- Session ID can change, for example -- and that old files get cleaned up regularly. These are hurdles, but can be overcome.

Within the limits of HTML, those are the options.
 
Share this answer
 
You can use application session to share the information between users.
 
Share this answer
 
I have used Session("whatever") to use variables throughout the entire codebehind.vb page, but is there an easier way? I mean, if I'm using several public functions within the codebehind.vb page, in order to pass the value of the variable from one function to the other, I noticed I always have to save the session and retrieve it on the other function and assign it to a variable within the scope of that function... any easier way (other than using static, as Patrik mentioned above.

Thanks!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900