Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to know whether to use static variables in C# website where multiple user access the website across same page.
Or to use view state/hidden field
or to use properties
Or to use simple variables
Which is most preferable and standard way to use ?
Thanks in advance.........
Posted
Comments
BillWoodruff 31-Dec-14 5:29am    
That would depend on what the purpose for using each static variables ... is.

What are your specific goals here: what do you wish to make available to every user ?
sp_suresh 31-Dec-14 5:46am    
Thanks ,I don't want to share values of one user to another .In our webform we have used lots of static variables and values being swapped from one user to another.We want to avoid this with standard and proper method .
Kornfeld Eliyahu Peter 31-Dec-14 5:32am    
Without actual info about your usage this question is incomplete...Please describe your use-case so we can help you!
sp_suresh 31-Dec-14 5:49am    
Here is View--thousands of user open same form at same time .In our code we used static variables at page level.To avoid this we set all these static variables at method level.But I am not sure whether values will be swapped or not?Please guide me proper solution.Thanks ...

1 solution

Don't.
Static variables aren't meant for that, and although they will work some of the time, you can't rely on them - particularly if you are using a webhosting service, or a server farm. Static variables are only "static" to that instance of the application - they are lost when the app is closed, and are not shared across instances of the app on separate servers (or even multiple instances on the same server).

Instead, keep "common" data in a database which is intended to be accessed by multiple people and fetch / update it there.
 
Share this answer
 
Comments
sp_suresh 31-Dec-14 5:55am    
Thanks ,but my question is that if these static variables are defined in methods then if multiple user access same method on same form will that value will be swaped?IF yes then We have to use viewstate variables to avoid swapping of values?
OriginalGriff 31-Dec-14 6:43am    
That's the problem: "yes, sometimes"
When a webpage is loaded, IIS starts the application if it isn't running, (most of the time - sometimes it starts a new one to be annoying) and if it is running then your page can load as a new class instance in the same application. Which means that static variables can be shared between users who load the same "page" in the same app instance. Sometimes. And sometimes they won't. And sometimes the app will be unloaded because of demand on the server, and...you get the idea. Personally, I wouldn't use static variables at all in a web page, to be on the safe side.

Instead, I'd use the Session - which is preserved across page loads - and which is unique to that user. If you want to share info, then use a DB.

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