473,499 Members | 1,525 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Static variables - saving state!

Hello,

I hate to admit it, but there have been times where I needed to persist a
value in an asp.net app, and instead of using properties {get:set} I simply
changed the variable to static - which maintained the value.

My question is - what are the potential hazards?

ie:

public static string firstName;
public string LastName;
private string PhoneNumber;

by doing this above, I can assign the fistName value, post back a page - and
still maintain the value..

Im sure (other than performance) I may be playing with danger here.

I have used static quit a bit on classes that do not need instances (mostly
my data classes use static method to access/modify data) .. this I understand.

What I dont exactly get is using static members in a non static class. If
someone could explain this to me better, that would be great.

thanks,
Nov 22 '05 #1
1 1195
I think that static variables in ASP.NET will have application scope and
exist until the website stops. If you need to maintain state this way, you
should probably use the Session object which will disappear when the session
dies.

"aiKeith" <ai*****@discussions.microsoft.com> wrote in message
news:32**********************************@microsof t.com...
Hello,

I hate to admit it, but there have been times where I needed to persist a
value in an asp.net app, and instead of using properties {get:set} I simply changed the variable to static - which maintained the value.

My question is - what are the potential hazards?

ie:

public static string firstName;
public string LastName;
private string PhoneNumber;

by doing this above, I can assign the fistName value, post back a page - and still maintain the value..

Im sure (other than performance) I may be playing with danger here.

I have used static quit a bit on classes that do not need instances (mostly my data classes use static method to access/modify data) .. this I understand.
What I dont exactly get is using static members in a non static class. If
someone could explain this to me better, that would be great.

thanks,

Nov 22 '05 #2

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

Similar topics

1
337
by: aiKeith | last post by:
Hello, I hate to admit it, but there have been times where I needed to persist a value in an asp.net app, and instead of using properties {get:set} I simply changed the variable to static -...
27
2109
by: Mr. Ed | last post by:
This should be easy, but I can't immediately see what the answer is... I have some code which needs to be re-entrant. It also needs to create a unique identifier. The obvious way to create a...
25
5116
by: Sahil Malik [MVP] | last post by:
So here's a rather simple question. Say in an ASP.NET application, I wish to share common constants as static variables in global.asax (I know there's web.config bla bla .. but lets just say I...
22
5055
by: yellow1912 | last post by:
Hi, I'm very new to C#, in fact I dont know anything about it. Im having problem with its static variables. I used static variables alot in C++, but seem like C# doesnt allow it inside a function...
4
1943
by: abCSharp | last post by:
I understand that static variables have app-domain scope. Till the app-domain is in memory, the static variable will be in memory. When are the app-domains unloaded is the question. I have read...
9
1848
by: Mark Broadbent | last post by:
Been a while since I've touched asp.net but one thing that always seems to fustrate me is the loss of state on variable declarations. Is there anyway (i.e. assigning an attribute etc) to instruct...
8
4844
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...
6
4083
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...
55
6149
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...
0
3947
by: Luis Zarrabeitia | last post by:
Quoting Joe Strout <joe@strout.net>: I'm sure your credentials are bigger than mine. But he is right. A lot of languages have ditched the "concept" of a static variable on a method (how do you...
0
7130
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
7007
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
7171
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
7220
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...
1
6893
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
7386
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
5468
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
3098
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
664
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.