Alex Rivera | Logout

Are static objects unique per user?

Asked 2010-09-23T21:09:27.000
12

I have a .net application (c#) that goes something like this

public partial class _Default : System.Web.UI.Page
{
    #region initial variables setup

    private static exam theExam;

    #endregion


    protected void Page_Load(object sender, EventArgs e)
    {
       if(!IsPostBack)
       {
        string userid = Request.Querystring["user"].ToString();
           theExam = new exam(userid, "some values");
       }
    }
// rest of code.

Now my question is, if user 105 logs in an instance of exam theExam is created and assign to the static declaration on top. If user 204 then logs in from a different computer, does the static object at the top gets the value of 204 even on user's 105's computer?

Edit
Report

2 Answers

12

No, the static object is the same instance for everyone logged on. Also the object doesn't live on 105's computer but just on the web server ofcourse.

answered 2010-09-23T21:10:46.197
6

Short answer: yes, the static field is global to the AppDomain, so doing this for one user will step on the data for another user.

You probably want to look into using Session storage instead, which is scoped per-user, e.g.

var theExam = Session["exam"] as Exam;
answered 2010-09-23T21:15:20.043

Your Answer