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

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 1192
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
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
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
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
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
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
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
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
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
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
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
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.