Static variables - saving state!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • aiKeith

    #1

    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,
  • Peter Rilling

    #2
    Re: Static variables - saving state!

    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" <aiKeith@discus sions.microsoft .com> wrote in message
    news:3203DB81-AA63-47C9-9162-8EBF70E0EF90@mi crosoft.com...[color=blue]
    > 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[/color]
    simply[color=blue]
    > 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 -[/color]
    and[color=blue]
    > 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[/color]
    (mostly[color=blue]
    > my data classes use static method to access/modify data) .. this I[/color]
    understand.[color=blue]
    >
    > 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,[/color]


    Comment

    Working...