473,387 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Scope of Static Members Across Sessions

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
Nov 15 '05 #1
2 5649
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" <ea******@hotmail.com> wrote in message
news:Oq****************@tk2msftngp13.phx.gbl...
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

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

"boxim" <sa*********@yahoo.co.uk> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
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" <ea******@hotmail.com> wrote in message
news:Oq****************@tk2msftngp13.phx.gbl...
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


Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Steve Holdoway | last post by:
Hi there, Is there any way of defining a variable who lives as long as the browser stays active? I did some initial tests, and found that the session variables did this, but now I've developed...
3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
4
by: Gery D. Dorazio | last post by:
Gurus, If a static variable is defined in a class what is the scope of the variable resolved to for it to remain 'static'? For instance, lets say I create a class library assembly that is...
0
by: Tony Wong | last post by:
I am trying to implement the Singleton Pattern for a assembly (class) which control a common resource on my computer. I need the Singleton behavior within a single process which contain multiple...
3
by: mark.norgate | last post by:
Hello I'm writing an application in ASP.NET 1.1 and have come across a problem using static fields in my page classes. I have lots of controls on the page that all need to bind to the same...
2
by: xzzy | last post by:
Using ASP.net, C# and a static class, does the following from MSDN mean: 1. a static hashtable would only live during the session and end when the session ends 2. there would be a different...
5
by: Steven Nagy | last post by:
Hi, I ran a simple test today. I created a web app that calls a static variable which self increments. I then accessed the same app from another machine and the variable increased, indicating...
3
by: =?Utf-8?B?TWFyaw==?= | last post by:
Hi... At least by the group title, this seems like a question for dotnet.framework.aspnet.caching but that group seems pretty slow. I'm trying to sort things out with a co-worker. We've got...
1
by: Giacomo Catenazzi | last post by:
Hello, To learn the details of C, I've build the following example, could you check if it is correct and if it miss some important cases? Are there some useful (real cases) examples of: -...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.