473,401 Members | 2,127 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,401 software developers and data experts.

static variable values in web apps

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 the value is remembered across sessions, across
machines, etc

public class StaticHolder
{
private static int _value = 0;
public static int Value
{
get
{
_value++;
return _value;
}
}
}

Is there a way to declare static variables that are static only for
the current web request? I don't want them to be instantiated, just
accessable from anywhere/anytime, for the current request. I want the
value to be GCed when the current request finishes.

Perhaps there is an attribute for this?

Thanks,
Steven

May 10 '07 #1
5 1995
Static variables, as you found, live on the application scope. Did you
consider session variables?

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
"Steven Nagy" <le*********@hotmail.comwrote in message
news:11**********************@y80g2000hsf.googlegr oups.com...
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 the value is remembered across sessions, across
machines, etc

public class StaticHolder
{
private static int _value = 0;
public static int Value
{
get
{
_value++;
return _value;
}
}
}

Is there a way to declare static variables that are static only for
the current web request? I don't want them to be instantiated, just
accessable from anywhere/anytime, for the current request. I want the
value to be GCed when the current request finishes.

Perhaps there is an attribute for this?

Thanks,
Steven

May 10 '07 #2
"Steven Nagy" <le*********@hotmail.comwrote in message
news:11**********************@y80g2000hsf.googlegr oups.com...
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 the value is remembered across sessions, across
machines, etc
That's right - static variables are created in the Application object,
meaning that there is only one instance of them across all Sessions...
Is there a way to declare static variables that are static only for
the current web request? I don't want them to be instantiated, just
accessable from anywhere/anytime, for the current request. I want the
value to be GCed when the current request finishes.
If you want a variable which exists once for each session, place it in the
Session object.

If you want a variable which exists once for each request (OK, for each page
request), place it in the ViewState object.
--
http://www.markrae.net

May 10 '07 #3
Consider using a global/public class which can be initialized at the page
load and can be used anywhere in your code and dispose it at the request end.

"Steven Nagy" wrote:
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 the value is remembered across sessions, across
machines, etc

public class StaticHolder
{
private static int _value = 0;
public static int Value
{
get
{
_value++;
return _value;
}
}
}

Is there a way to declare static variables that are static only for
the current web request? I don't want them to be instantiated, just
accessable from anywhere/anytime, for the current request. I want the
value to be GCed when the current request finishes.

Perhaps there is an attribute for this?

Thanks,
Steven

May 10 '07 #4
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 the value is remembered across sessions, across
machines, etc
public class StaticHolder
{
private static int _value = 0;
public static int Value
{
get
{
_value++;
return _value;
}
}
}
Is there a way to declare static variables that are static only for
the current web request? I don't want them to be instantiated, just
accessable from anywhere/anytime, for the current request. I want the
value to be GCed when the current request finishes.

Perhaps there is an attribute for this?

Thanks,
Steven
You can add values to the Context object:
Context.Items["key"] = somevalue;
or
System.Web.HttpContext.Current.Items["key"] = somevalue;

and retrieve it in a similar way.
This value will live for the duration of a single request!

Hans Kesting
May 10 '07 #5
Steven Nagy wrote:
Is there a way to declare static variables that are static only for
the current web request? I don't want them to be instantiated, just
accessable from anywhere/anytime, for the current request. I want the
value to be GCed when the current request finishes.
You could use the ThreadStatic attribute to create a static variable
that is local for the thread. I would advice against it, though, as the
value will only be initialised for the first thread. After that the
initial value will be undefined, but the compiler will not warn you if
you are using the undefined value. Also, the value will never go out of
scope, so you have to make sure to always set the value to null for the
garbage collection to work.

Why not just use a member variable in the class for the page? It's life
span is the same as the request and it's always initialised. It's not as
easily reachable from anywhere as you need a reference to the current
page object to reach the variable, but is that really a problem?

--
Göran Andersson
_____
http://www.guffa.com
May 10 '07 #6

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

Similar topics

7
by: ank | last post by:
Hi, I was curious about how to define static data member of template class. Should I put the definition in a separate source file or in the same header file as its template class? And when this...
6
by: Michael B Allen | last post by:
I want to initialize a static variable to a "random" value like: static void * get_key(struct dnsreq *req) { static uint16_t next_txnid = (uint32_t)req & 0xFFFF; But gcc gives me an error: ...
4
by: Dave | last post by:
I used the following class and .aspx code below to understand how static works on variables and methods taken from...
8
by: Simone Chiaretta | last post by:
I've a very strange behaveour related to a website we built: from times to times, something should happen on the server, and all static variables inside the web application, both defined inside aspx...
28
by: Dennis | last post by:
I have a function which is called from a loop many times. In that function, I use three variables as counters and for other purposes. I can either use DIM for declaring the variables or Static. ...
9
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
3
by: Steve Folly | last post by:
Hi, I had a problem in my code recently which turned out to be the 'the "static initialization order fiasco"' problem (<http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12>) The FAQ...
16
by: RB | last post by:
Hi clever people :-) I've noticed a lot of people stating not to use static variables with ASP.NET, and, as I understand it, the reason is because the variable is shared across user sessions -...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.