I am having one doubt regarding the use of static variable in Asp.net pages.
I am having one page say UserDetails.aspx. In this page, I have one static variable to s
It will be shared application wide just like Application["some_id"].
Use normal int variable for this.
As Adeel already mentioned: static(or shared in VB.Net) variables are application-wide. That means they are the same for every user and exist till webserver is stopped or last session has abandoned.
You could use the Session to store variables that belong to the current User.
If you need access to other user's numberOfReviews(f.e. as administrator or for statistics), you could use database, asp.net-cache or a simple static dictionary with the userid as key.
usually to do this a database is used
Data Table
userId | UserViews
Also you can use static variable as u are saying in that case you have to store data in Application State, problem with that is, ur whole data will be resett whenever ur will re-start.
static variable scope is application wide. numberOfReviews
will be shared among all users. you need to use Session
to store per user, so it is accessible in all pages. On the other hand, if you just need it on a specific page, you can save it in ViewState
and can get it in post back.
Application Scope: The variables that have application scope are available throughout the application, i.e to all users of the applications across all pages.
Session Scope: When many users connect to your site, each of them will have a separate session (tied to the identity of the user that is recognized by the application.) When the variable has session scope it will have new instance for each session, even though the users are accessing the same page. The session variable instance is available across all pages for that session.
Page Scope: When you have a instance variable on a Page it is specific to that page only and that session only.
Static variables have Application scope
. All users of the application will share the same variable instance in your case.
Please note that although static variables have one instance in the app domain. So if you have your application deployed on a load balanced web farm, each app domain will have a separate instance of the variable. This might give you incorrect result.
Based on this you should decide what scope your variable should be in. IMO, using static variables is a code smell and should be discouraged.