I’m having a bit of a weird problem to do with scope of variables. I’ve declared a variable in the following way:
public partial class MyClass: System.Web.UI.Page
{
protected static int MyGlobalVariable;
protected void MyFunction()
{
MyGlobalVariable = 1;
}
}
And this works fine on the workings of my page. However when two users are using the same page I’m finding that I’m getting cross over. If one user were to set the variable to 5 and the other use then accessed that variable it would be set to 5. How can I set the variable so it’s only accessible to the user who originally set it?
If you declare
MyGlobalVariableas static, then only one instance of it will exist for all instances of the class, so as you said, multiple users, on multiple instances of teh same page will be accessing the same value.either declare the int without the
staticmodifier or if you need it to persist for that user, consider usingViewstate(for page scope) orSession(for session scope)e.g.
or