Scope of Static Members Across Sessions

  • Thread starter Thread starter Earl Teigrob
  • Start date Start date
E

Earl Teigrob

I am using C# to program ASP.NET. I am using class of static fields to hold
all variables that are common to the application, and I am sure that this is
fine. However, I was also using some static fields to hold values that
calculated every time a user opens a page (unique to each session). These
static fields are used in other classes thoughout the application. However,
it suddenlly struck me that these static fields are (i think) common across
all user sessions so that they may confict when two(or more) users try to
open a page that sets and accesses these fields at the same time. Is that
correct?

I am thinking that I should be using instance field variables for all
variables where the data is unique to that session? Is that correct?

Thanks for the help

Earl
 
Yes, they're common across the application. (not Session)

I've just done exactly the same thing

got a class with static members that are used in classes not derived from
Web.UI.Page
also contain Session specific stuff in instance variables

i.e.

class mystateclass {

protected string _username;

public static string DatabaseServer {
get {
// gets from config file
}
}

public string CurrentUsername {
get {
return _username;
}
}

///////

// in Session_Start

Session["stateinfo"] = new mystateclass();


HTH
sam
 
Actually, the static variables don't apply to the application, they
apply to the whole app-domain, which the Application is just a part of.
Each web app on IIS runs in its own application domain. Granted, there is
only one Application object per web app, but it is an important distinction,
because static variables are tied to one, and not the other.


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

boxim said:
Yes, they're common across the application. (not Session)

I've just done exactly the same thing

got a class with static members that are used in classes not derived from
Web.UI.Page
also contain Session specific stuff in instance variables

i.e.

class mystateclass {

protected string _username;

public static string DatabaseServer {
get {
// gets from config file
}
}

public string CurrentUsername {
get {
return _username;
}
}

///////

// in Session_Start

Session["stateinfo"] = new mystateclass();


HTH
sam

Earl Teigrob said:
I am using C# to program ASP.NET. I am using class of static fields to hold
all variables that are common to the application, and I am sure that
this
is
fine. However, I was also using some static fields to hold values that
calculated every time a user opens a page (unique to each session). These
static fields are used in other classes thoughout the application. However,
it suddenlly struck me that these static fields are (i think) common across
all user sessions so that they may confict when two(or more) users try to
open a page that sets and accesses these fields at the same time. Is that
correct?

I am thinking that I should be using instance field variables for all
variables where the data is unique to that session? Is that correct?

Thanks for the help

Earl
 
Back
Top