Trying to understand static and when to use it?

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);
}
 
N

Nicholas Paldino [.NET/C# MVP]

Dave,

In this case, you will want to place a lock statement around the
statement:

++employeeCounter;

The reason for this is that pages in ASP.NET are processed on different
threads. Because of this, you will need to protect the variable.

Static values are stored across the application domain. In ASP.NET, you
have one application domain running per web application, not per user
accessing the application. For this reason, you need to protect these
variables.

As for when to use static variables, in ASP.NET, I would use them when
you need to store application-wide information. It is even more important
in ASP.NET to make sure that the variable is protected for multithreaded
access (since pages are all going to be processed on different threads).

Hope this helps.
 
G

Guest

Nicholas,

Thanks for the reply. To futher clarify an example of the method is:

A static method that accepts a ticker symbol as a string and makes and
HttpWebRequest to a url that returns a company description and stock prices
as Xml. Within this method, I then parse the XML and return only the filings
from the the method.

Just to clarify, what if two users call this at the same time? Since the
method is static, is any data within this method (parameters passed in and
local variables declared within the method) also considered static and shared
for all users of the class?

From what you are saying, I'm not sure when static would be a benefit to use
in ASP.NET.

Nicholas Paldino said:
Dave,

In this case, you will want to place a lock statement around the
statement:

++employeeCounter;

The reason for this is that pages in ASP.NET are processed on different
threads. Because of this, you will need to protect the variable.

Static values are stored across the application domain. In ASP.NET, you
have one application domain running per web application, not per user
accessing the application. For this reason, you need to protect these
variables.

As for when to use static variables, in ASP.NET, I would use them when
you need to store application-wide information. It is even more important
in ASP.NET to make sure that the variable is protected for multithreaded
access (since pages are all going to be processed on different threads).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Dave said:
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);
}
 
N

Nicholas Paldino [.NET/C# MVP]

Dave,

In this case, if your method is not accessing other static variables,
and is only working with references/values that are stored on the stack (in
other words, declared in the function), then you should have nothng to worry
about. The only thing you have to worry about at this point is other
resources you might be accessing (for example, the same file, if you are
working with files).

As for the use of static, this sounds like a good one. It doesn't sound
like it needs any kind of specialization which would require individual
instances.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Dave said:
Nicholas,

Thanks for the reply. To futher clarify an example of the method is:

A static method that accepts a ticker symbol as a string and makes and
HttpWebRequest to a url that returns a company description and stock
prices
as Xml. Within this method, I then parse the XML and return only the
filings
from the the method.

Just to clarify, what if two users call this at the same time? Since the
method is static, is any data within this method (parameters passed in and
local variables declared within the method) also considered static and
shared
for all users of the class?

From what you are saying, I'm not sure when static would be a benefit to
use
in ASP.NET.

Nicholas Paldino said:
Dave,

In this case, you will want to place a lock statement around the
statement:

++employeeCounter;

The reason for this is that pages in ASP.NET are processed on
different
threads. Because of this, you will need to protect the variable.

Static values are stored across the application domain. In ASP.NET,
you
have one application domain running per web application, not per user
accessing the application. For this reason, you need to protect these
variables.

As for when to use static variables, in ASP.NET, I would use them
when
you need to store application-wide information. It is even more
important
in ASP.NET to make sure that the variable is protected for multithreaded
access (since pages are all going to be processed on different threads).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Dave said:
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);
}
 
G

Guest

Nicholas,

Thanks! One last thing. By converting other methods similar to this over to
static, what does one really gain besides less code in that I don't have to
create instances of each class? I'm guessing static uses less resources and
helps performance in the long run.

Dave.

Nicholas Paldino said:
Dave,

In this case, if your method is not accessing other static variables,
and is only working with references/values that are stored on the stack (in
other words, declared in the function), then you should have nothng to worry
about. The only thing you have to worry about at this point is other
resources you might be accessing (for example, the same file, if you are
working with files).

As for the use of static, this sounds like a good one. It doesn't sound
like it needs any kind of specialization which would require individual
instances.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Dave said:
Nicholas,

Thanks for the reply. To futher clarify an example of the method is:

A static method that accepts a ticker symbol as a string and makes and
HttpWebRequest to a url that returns a company description and stock
prices
as Xml. Within this method, I then parse the XML and return only the
filings
from the the method.

Just to clarify, what if two users call this at the same time? Since the
method is static, is any data within this method (parameters passed in and
local variables declared within the method) also considered static and
shared
for all users of the class?

From what you are saying, I'm not sure when static would be a benefit to
use
in ASP.NET.

Nicholas Paldino said:
Dave,

In this case, you will want to place a lock statement around the
statement:

++employeeCounter;

The reason for this is that pages in ASP.NET are processed on
different
threads. Because of this, you will need to protect the variable.

Static values are stored across the application domain. In ASP.NET,
you
have one application domain running per web application, not per user
accessing the application. For this reason, you need to protect these
variables.

As for when to use static variables, in ASP.NET, I would use them
when
you need to store application-wide information. It is even more
important
in ASP.NET to make sure that the variable is protected for multithreaded
access (since pages are all going to be processed on different threads).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

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);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top