Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Static Varible Sharing Problem

Static Varible Sharing Problem

Scheduled Pinned Locked Moved ASP.NET
helpdesigntutorial
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Shivan Nandan
    wrote on last edited by
    #1

    Hai Friends I have declared a variable outside all the function in an aspx.cs page, like

    public partial class StaticVar : System.Web.UI.Page
    {

    static int a;
    
    protected void Page\_Load(object sender, EventArgs e)
    {
    
        Response.Write(a.ToString());
    }
    protected void Button1\_Click(object sender, EventArgs e)
    {
        a = 50;
        Response.Write("set 50, a= " + a.ToString());
    }
    protected void Button2\_Click(object sender, EventArgs e)
    {
        a = 60;
        Response.Write("set 60, a= " + a.ToString());
    }
    protected void Button3\_Click(object sender, EventArgs e)
    {
        Response.Write("a= " + a.ToString());
    }
    

    }

    The problem is that when more than one user access this page and modifies the variable a, all the users gets the new value for a, i.e., they loose the value what they had set for a. They get the value that is been set by the last user. If I remove 'Static' from its declaration, then its value is not available to other functions. How to solve this problem. Please help me.

    Shivanandan C V

    C S C 3 Replies Last reply
    0
    • S Shivan Nandan

      Hai Friends I have declared a variable outside all the function in an aspx.cs page, like

      public partial class StaticVar : System.Web.UI.Page
      {

      static int a;
      
      protected void Page\_Load(object sender, EventArgs e)
      {
      
          Response.Write(a.ToString());
      }
      protected void Button1\_Click(object sender, EventArgs e)
      {
          a = 50;
          Response.Write("set 50, a= " + a.ToString());
      }
      protected void Button2\_Click(object sender, EventArgs e)
      {
          a = 60;
          Response.Write("set 60, a= " + a.ToString());
      }
      protected void Button3\_Click(object sender, EventArgs e)
      {
          Response.Write("a= " + a.ToString());
      }
      

      }

      The problem is that when more than one user access this page and modifies the variable a, all the users gets the new value for a, i.e., they loose the value what they had set for a. They get the value that is been set by the last user. If I remove 'Static' from its declaration, then its value is not available to other functions. How to solve this problem. Please help me.

      Shivanandan C V

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Buy a basic book on ASP.NET and read it. Actually, I suspect your issue is that you don't understand ASP.NET. Static works just as you are seeing, and non static only exists for the lifecycle of one page. Therefore, you need to store values in something persistent, such as the session, or if it's all in one page, viewstate.

      Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp

      1 Reply Last reply
      0
      • S Shivan Nandan

        Hai Friends I have declared a variable outside all the function in an aspx.cs page, like

        public partial class StaticVar : System.Web.UI.Page
        {

        static int a;
        
        protected void Page\_Load(object sender, EventArgs e)
        {
        
            Response.Write(a.ToString());
        }
        protected void Button1\_Click(object sender, EventArgs e)
        {
            a = 50;
            Response.Write("set 50, a= " + a.ToString());
        }
        protected void Button2\_Click(object sender, EventArgs e)
        {
            a = 60;
            Response.Write("set 60, a= " + a.ToString());
        }
        protected void Button3\_Click(object sender, EventArgs e)
        {
            Response.Write("a= " + a.ToString());
        }
        

        }

        The problem is that when more than one user access this page and modifies the variable a, all the users gets the new value for a, i.e., they loose the value what they had set for a. They get the value that is been set by the last user. If I remove 'Static' from its declaration, then its value is not available to other functions. How to solve this problem. Please help me.

        Shivanandan C V

        S Offline
        S Offline
        saanj
        wrote on last edited by
        #3

        There is no need to declare it static if you are declaring it as class level variable. As a class level variable, it's value should be accessible in all methods inside that class. Regards Saanj

        Either you love IT or leave IT...

        C S 2 Replies Last reply
        0
        • S saanj

          There is no need to declare it static if you are declaring it as class level variable. As a class level variable, it's value should be accessible in all methods inside that class. Regards Saanj

          Either you love IT or leave IT...

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          But not between postbacks.

          Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp

          1 Reply Last reply
          0
          • S Shivan Nandan

            Hai Friends I have declared a variable outside all the function in an aspx.cs page, like

            public partial class StaticVar : System.Web.UI.Page
            {

            static int a;
            
            protected void Page\_Load(object sender, EventArgs e)
            {
            
                Response.Write(a.ToString());
            }
            protected void Button1\_Click(object sender, EventArgs e)
            {
                a = 50;
                Response.Write("set 50, a= " + a.ToString());
            }
            protected void Button2\_Click(object sender, EventArgs e)
            {
                a = 60;
                Response.Write("set 60, a= " + a.ToString());
            }
            protected void Button3\_Click(object sender, EventArgs e)
            {
                Response.Write("a= " + a.ToString());
            }
            

            }

            The problem is that when more than one user access this page and modifies the variable a, all the users gets the new value for a, i.e., they loose the value what they had set for a. They get the value that is been set by the last user. If I remove 'Static' from its declaration, then its value is not available to other functions. How to solve this problem. Please help me.

            Shivanandan C V

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #5

            Shivan Nandan wrote:

            The problem is that when more than one user access this page and modifies the variable a, all the users gets the new value for a, i.e., they loose the value what they had set for a. They get the value that is been set by the last user.

            That is the correct functionality. ASP.NET applications are multi-user applications. static variables are global for the entire application. If you set it to some value then all users will see that value.

            Shivan Nandan wrote:

            If I remove 'Static' from its declaration, then its value is not available to other functions.

            Incorrect. Its value is available to the methods in the class. This time however, the value only lasts as long as the object it is in lasts (in this case the object is the one representing your page).

            Shivan Nandan wrote:

            How to solve this problem.

            Looking at your sample code, you expect the value to hang around for postbacks. That isn't going to happen. The page object (along with your variable) will exist for as long as it takes to process and render the page. The page object is them discarded (along with your variable). So, basically, you need some mechanism to store the value between postbacks. You can store the value in the page itself (a hidden field, or add it to the viewstate). You could store it in the session. You could create your own mechanism. It is up to you. Ultimately, you need ot read a book on how ASP.NET works, and especially how the page lifecycle works as this leaky abstraction trips up many people. In other words HTTP is stateless, but ASP.NET tries to provide a stateful framework, and sometimes it just doesn't work they way you'd expect. So you have to go and read some stuff about the page lifecycle to understand what is going on.

            Man who stand on hill with mouth open wait long time for roast duck to drop in

            S 1 Reply Last reply
            0
            • S saanj

              There is no need to declare it static if you are declaring it as class level variable. As a class level variable, it's value should be accessible in all methods inside that class. Regards Saanj

              Either you love IT or leave IT...

              S Offline
              S Offline
              Shivan Nandan
              wrote on last edited by
              #6

              If I don't declare it as Static, then its value is retained only in the function in which it is assigned. In other function its value becomes 0. Please check my code without Static identifier.

              Shivanandan C V

              C S 2 Replies Last reply
              0
              • S Shivan Nandan

                If I don't declare it as Static, then its value is retained only in the function in which it is assigned. In other function its value becomes 0. Please check my code without Static identifier.

                Shivanandan C V

                C Offline
                C Offline
                Colin Angus Mackay
                wrote on last edited by
                #7

                Shivan Nandan wrote:

                If I don't declare it as Static, then its value is retained only in the function in which it is assigned. In other function its value becomes 0. Please check my code without Static identifier.

                The issue is that without the static the variable is only available for that one instance of the page. Every time you visit the page you get a new instance of the page so the variable is set back to what it started as. Your mental model is that when a user gets a page they retain that page object for the duration of their session. They don't. That would place an huge strain on the server to maintain all that for all users. A page will be destroyed soon after it is rendered - along with all variables on it.

                Man who stand on hill with mouth open wait long time for roast duck to drop in

                1 Reply Last reply
                0
                • S Shivan Nandan

                  If I don't declare it as Static, then its value is retained only in the function in which it is assigned. In other function its value becomes 0. Please check my code without Static identifier.

                  Shivanandan C V

                  S Offline
                  S Offline
                  saanj
                  wrote on last edited by
                  #8

                  Use Session variable. Regards Saanj

                  Either you love IT or leave IT...

                  1 Reply Last reply
                  0
                  • C Colin Angus Mackay

                    Shivan Nandan wrote:

                    The problem is that when more than one user access this page and modifies the variable a, all the users gets the new value for a, i.e., they loose the value what they had set for a. They get the value that is been set by the last user.

                    That is the correct functionality. ASP.NET applications are multi-user applications. static variables are global for the entire application. If you set it to some value then all users will see that value.

                    Shivan Nandan wrote:

                    If I remove 'Static' from its declaration, then its value is not available to other functions.

                    Incorrect. Its value is available to the methods in the class. This time however, the value only lasts as long as the object it is in lasts (in this case the object is the one representing your page).

                    Shivan Nandan wrote:

                    How to solve this problem.

                    Looking at your sample code, you expect the value to hang around for postbacks. That isn't going to happen. The page object (along with your variable) will exist for as long as it takes to process and render the page. The page object is them discarded (along with your variable). So, basically, you need some mechanism to store the value between postbacks. You can store the value in the page itself (a hidden field, or add it to the viewstate). You could store it in the session. You could create your own mechanism. It is up to you. Ultimately, you need ot read a book on how ASP.NET works, and especially how the page lifecycle works as this leaky abstraction trips up many people. In other words HTTP is stateless, but ASP.NET tries to provide a stateful framework, and sometimes it just doesn't work they way you'd expect. So you have to go and read some stuff about the page lifecycle to understand what is going on.

                    Man who stand on hill with mouth open wait long time for roast duck to drop in

                    S Offline
                    S Offline
                    Shivan Nandan
                    wrote on last edited by
                    #9

                    Thank you very much for helping me...

                    Shivanandan C V

                    modified on Wednesday, June 10, 2009 4:17 AM

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups