473,564 Members | 2,749 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 1205
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*****@discus sions.microsoft .com> wrote in message
news:32******** *************** ***********@mic rosoft.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 - which maintained the value. My question is - what are the potential hazards? ie:
27
2119
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 unique ID is to maintain a static counter, and to increment it each time it's needed. Any ideas on how to do this in a re-entrant way? I don't call...
25
5134
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 wanna use global.asax) --- Would you declare your static var as --- public static int x ;
22
5064
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 the way C# do, so how can you handle recursive functions that need static variable. One more thing, I have a variable like this: OracleDataReader...
4
1949
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 somewhere in this forum that statics live till the asp.net process is recycled. Please clarify if someone can. Thanx! "David Jessee" wrote: > I'd...
9
1856
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 the server to remember a variables state *without* having to go through the rigmarole of saving and loading to and from the Session state manually or...
8
4853
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 differences in the way both the objects are handled by IIS. Are both objects stored in different memory spaces? I can access both the objects in my web...
6
4106
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 memory management and connection pooling constructs) as static variables which were intended to scoped to the process. 2. Managed C++ assemblies...
55
6171
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 C# in some way? Or maybe no, because it is similar to a global variable (with its scope restricted) which C# is dead against? Zytan
0
3984
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 parse that sentence, btw?) in favour of using encapsulation. Of the top of my head, there is Java, C# and Python, and I think even PHP and Perl....
0
7666
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8108
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7644
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7951
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5484
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3643
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1201
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.