471,570 Members | 965 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,570 software developers and data experts.

Trying to understand static and when to use it?

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);
}
Nov 17 '05 #1
4 1742
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]
- mv*@spam.guard.caspershouse.com

"Dave" <Da**@discussions.microsoft.com> wrote in message
news:3A**********************************@microsof t.com...
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);
}

Nov 17 '05 #2
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 [.NET/C# MVP]" wrote:
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]
- mv*@spam.guard.caspershouse.com

"Dave" <Da**@discussions.microsoft.com> wrote in message
news:3A**********************************@microsof t.com...
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);
}


Nov 17 '05 #3
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]
- mv*@spam.guard.caspershouse.com

"Dave" <Da**@discussions.microsoft.com> wrote in message
news:5E**********************************@microsof t.com...
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 [.NET/C# MVP]" wrote:
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]
- mv*@spam.guard.caspershouse.com

"Dave" <Da**@discussions.microsoft.com> wrote in message
news:3A**********************************@microsof t.com...
>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);
> }


Nov 17 '05 #4
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 [.NET/C# MVP]" wrote:
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]
- mv*@spam.guard.caspershouse.com

"Dave" <Da**@discussions.microsoft.com> wrote in message
news:5E**********************************@microsof t.com...
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 [.NET/C# MVP]" wrote:
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]
- mv*@spam.guard.caspershouse.com

"Dave" <Da**@discussions.microsoft.com> wrote in message
news:3A**********************************@microsof t.com...
>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);
> }


Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Steven D'Aprano | last post: by
51 posts views Thread by Richard Hengeveld | last post: by
4 posts views Thread by Deniz Bahar | last post: by
12 posts views Thread by Ron Weldy | last post: by
3 posts views Thread by Michael B. Trausch | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by Vinnie | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.