18

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 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?

numberOfReviews

5 Answers 5

26

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.

2
  • I was wondering about the scope of static variables for web code, and your answer explain it very well. Thanks for breaking it down.
    – PhaZ90771
    Jun 9, 2014 at 19:10
  • Cool, now I know that I can use static variables for sample data in small proofs of concept where persistence is not very important (and session is not at hand), just what I needed :) Jan 26, 2015 at 16:57
18

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.

2
  • Where ViewState is pagewide, Session is "per current user session" Mar 7, 2011 at 9:07
  • @Davide: ViewState is page-wide but it's also important to say that it belongs to current user only too. Mar 7, 2011 at 9:12
2

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

Use normal int variable for this.

1

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.

2
  • Isn't the Asp.net cache also global and therefore shared between users as well. I guess combined with the dictionary you could get the desired result. Jun 20, 2012 at 14:12
  • @KingOfHypocrites: I'm afraid that i don't understand. Yes, the cache is application wide and not user specific. That's why I've mentioned that he can use it (or the database, static dictionary, ...) if he wants to access other user's properties(f.e. numberOfReviews). Since that's a special case (f.e. an administrator view) it's not as dangerous to use a static/cached object. Jun 20, 2012 at 14:29
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.