Static variable in page

I am having one doubt regarding the use of static variable in pages.

I am having one page say UserDetails.aspx.
In this page, I have one static variable to store some data specific to a user. So, will this variable be shared across multiple user or a separate variable will be created for each user?

  public partial class UserDetails : System.Web.UI.Page
       static int numberOfReviews=0;
       protected void Page_Load(object sender, EventArgs e)
            numberOfReviews= GetReviews();

Here, will numberOfReviews be specific to each user or will be shared?



Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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.

Method 3

It will be shared application wide just like Application[“some_id”].

Use normal int variable for this.

Method 4

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, or a simple static dictionary with the userid as key.

Method 5

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.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x