G
Guest
I used the following class and .aspx code below to understand how static
works on variables and methods taken from
"http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfstaticpg.asp".
I opened up two instances of my browser to simulate two users accessing the
page at the same time by pointing to Employee.aspx below. Refreshing several
times, the counter value is increased and shared between both browser
instances.
If shared variables are shared between two instances of the app running in
two browsers, My question is what happens when a static method, that accepts
parameters, is called at the same time by two users. Are the parameter
values going to be isolated from each other? What about variable declared and
used within the static method itself? I'm trying to determine what are valid
rules for making a method static or things I need to be aware of when doing
so.
My code:
using System;
namespace StaticExample
{
/// <summary>
/// Summary description for Employee.
/// </summary>
public class Employee
{
public string id;
public string name;
public Employee ()
{
}
public Employee (string name, string id)
{
this.name = name;
this.id = id;
AddEmployee();
}
public static int employeeCounter;
public static int AddEmployee()
{
return ++employeeCounter;
}
}
}
The Employee.aspx page calls the above...
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
Employee employee;
employee = new Employee("Test User", "1");
Response.Write(Employee.employeeCounter);
}
works on variables and methods taken from
"http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfstaticpg.asp".
I opened up two instances of my browser to simulate two users accessing the
page at the same time by pointing to Employee.aspx below. Refreshing several
times, the counter value is increased and shared between both browser
instances.
If shared variables are shared between two instances of the app running in
two browsers, My question is what happens when a static method, that accepts
parameters, is called at the same time by two users. Are the parameter
values going to be isolated from each other? What about variable declared and
used within the static method itself? I'm trying to determine what are valid
rules for making a method static or things I need to be aware of when doing
so.
My code:
using System;
namespace StaticExample
{
/// <summary>
/// Summary description for Employee.
/// </summary>
public class Employee
{
public string id;
public string name;
public Employee ()
{
}
public Employee (string name, string id)
{
this.name = name;
this.id = id;
AddEmployee();
}
public static int employeeCounter;
public static int AddEmployee()
{
return ++employeeCounter;
}
}
}
The Employee.aspx page calls the above...
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
Employee employee;
employee = new Employee("Test User", "1");
Response.Write(Employee.employeeCounter);
}