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?
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.
Lifetime of static variables and user sessions are very different concepts. Static variables have a lifetime defined by the CLR and essentially boils down to the following 2 rules
AppDomain
I'm finding it hard to write the second rule without it being ambiguous as to unique. Essentially
MyType<int>
andMyType<string>
each have different static variables. WhileMyType<int>
andMyType<int>
share the same one.User's acess to a web server don't affect either of these.
If you want to have per user data then use the
Session
to store the data.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.
There is one "instance" of a static object per AppDomain. So the answer to your question is yes. Since you overwrite the variable when user 204 logs in, the same value will appear for user 105, too.
Some general advices
Session["exam"] = currentUser.Exam;
There's also a [ThreadStatic] attribute in .Net that will make one static instance per thread.
http://msdn.microsoft.com/en-us/library/system.threadstaticattribute(VS.71).aspx