Ashwani K
Ashwani K

Reputation: 7980

Static variable in asp.net page

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

Upvotes: 18

Views: 19714

Answers (5)

MRG
MRG

Reputation: 3219

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

Use normal int variable for this.

Upvotes: 2

Unmesh Kondolikar
Unmesh Kondolikar

Reputation: 9312

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.

Upvotes: 26

Adeel
Adeel

Reputation: 19228

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.

Upvotes: 18

Tim Schmelter
Tim Schmelter

Reputation: 460018

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.

Upvotes: 1

Praveen Prasad
Praveen Prasad

Reputation: 32107

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.

Upvotes: 0

Related Questions