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

How are static variables handled in asp.net

I use static variables in my asp.net apps instead of application variables
to store global parameters, its worked well for me so far but I was
wondering if it is efficient. Does anyone know how static variables are
handled in asp.net?

--
Frank Wisniewski MCSE 4.0, MCP+I, A+
f p w 2 3 @ h o t m a i l . c o m
Nov 18 '05 #1
5 2515
Hi Frank:

If the static variables are readonly, then they can be much friendlier
than using the Application collection. For one, you don't have the
overhead of locking that the Application collection does for you, and
secondly they are type safe and you don't have to do any casting or
worry about boxing.

If you need to read and write to static variables take care, because
ASP.NET is a multithreaded environment. The Application collection
does locking for you during a Get or a Set with a reader / writer
lock, but using statics you are on your own.

--
Scott
http://www.OdeToCode.com

On Thu, 9 Sep 2004 12:57:50 -0500, "Frank Wisniewski"
<fp***@hotmail.com> wrote:
I use static variables in my asp.net apps instead of application variables
to store global parameters, its worked well for me so far but I was
wondering if it is efficient. Does anyone know how static variables are
handled in asp.net?


Nov 18 '05 #2
How do you use static variables?

Is that like global variables that are always available?

-Frank
"Frank Wisniewski" <fp***@hotmail.com> wrote in message
news:41********@Usenet.com...
I use static variables in my asp.net apps instead of application variables
to store global parameters, its worked well for me so far but I was
wondering if it is efficient. Does anyone know how static variables are
handled in asp.net?

--
Frank Wisniewski MCSE 4.0, MCP+I, A+
f p w 2 3 @ h o t m a i l . c o m

Nov 18 '05 #3
I agree with everything Scott said.

If you are after readonly values, you should also consider using a
configuration file, namely the web.config with your own configuration
section (http://www.openmymind.net/Configuration/index.html). Its good
practice to closely examine hard-coded to see if they should be, well
hard-coded.

As Scott said, you can run into nasty stuff if you are reading and writing
to and from them. If this is the case, i would definitely avoid static
variables...I would also avoid the Application object, and use a database
with Caching (I hate the application object ;) )

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:rr********************************@4ax.com...
Hi Frank:

If the static variables are readonly, then they can be much friendlier
than using the Application collection. For one, you don't have the
overhead of locking that the Application collection does for you, and
secondly they are type safe and you don't have to do any casting or
worry about boxing.

If you need to read and write to static variables take care, because
ASP.NET is a multithreaded environment. The Application collection
does locking for you during a Get or a Set with a reader / writer
lock, but using statics you are on your own.

--
Scott
http://www.OdeToCode.com

On Thu, 9 Sep 2004 12:57:50 -0500, "Frank Wisniewski"
<fp***@hotmail.com> wrote:
I use static variables in my asp.net apps instead of application variablesto store global parameters, its worked well for me so far but I was
wondering if it is efficient. Does anyone know how static variables are
handled in asp.net?

Nov 18 '05 #4
Hi Frank:

If I have this class in an ASP.NET project:

public class Foo
{
public static string Bar
{
get
{
// pull a string from the config file,
// the database, a resource file, etc..
}
}
}

Then I can write:

Foo.Bar

from anywhere else in code to pull back the string I need. Public
static members are global and always available (which can make them a
double edged sword in some scenarios).

VB.NET works the same way, except the keyword is "Shared" instead of
static.

--
Scott
http://www.OdeToCode.com
On Thu, 9 Sep 2004 14:14:10 -0400, "Frank Mamone"
<fr**********@canada.com> wrote:
How do you use static variables?

Is that like global variables that are always available?

-Frank


Nov 18 '05 #5
Thanks!

"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:1a********************************@4ax.com...
Hi Frank:

If I have this class in an ASP.NET project:

public class Foo
{
public static string Bar
{
get
{
// pull a string from the config file,
// the database, a resource file, etc..
}
}
}

Then I can write:

Foo.Bar

from anywhere else in code to pull back the string I need. Public
static members are global and always available (which can make them a
double edged sword in some scenarios).

VB.NET works the same way, except the keyword is "Shared" instead of
static.

--
Scott
http://www.OdeToCode.com
On Thu, 9 Sep 2004 14:14:10 -0400, "Frank Mamone"
<fr**********@canada.com> wrote:
How do you use static variables?

Is that like global variables that are always available?

-Frank

Nov 18 '05 #6

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

Similar topics

6
by: jacob navia | last post by:
As far as I understood the standard, non-automatic variables (static/global data) can't be cached in registers because in a multiprocessing or multi-threading environment, another thread/processor...
9
by: AnandRaj | last post by:
Hi guys, I have a few doubts in C. 1. Why static declartions are not allowed inside structs? eg struct a { static int i; }; Throws an error ..
8
by: Vishwanathan Raman | last post by:
Hi I have a declared a static DataSet object SOBJ in Global.asax.I also have a localy defined DataSet LSOBJ in Global.asax which I am storing in Application State.Is there any technical...
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. ...
6
by: Vladislav Kosev | last post by:
I have this strange problem now twice: I am writing this relatevely large web site on 2.0 and I made a static class, which I use for url encoding and deconding (for remapping purposes). This static...
4
by: Stephen Walch | last post by:
Our application environment consists of three basic layers: 1. Third-party unmanaged DLLs that were written before the CLR was invented and maintain a significant amount of information (including...
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...
1
by: Rahul | last post by:
Hi Everyone, It is known that static member variables can be accessed without creating an instance of the class and it serves as a global variable without polluting the global name space. So...
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
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...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.