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. General Programming
  3. C#
  4. static variable

static variable

Scheduled Pinned Locked Moved C#
questioncsharpasp-net
9 Posts 3 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.
  • G Offline
    G Offline
    goatstudio
    wrote on last edited by
    #1

    I define some of my variables in asp.net page as a static variable, to keep track of the value when do a postback. For exmaple protected void Page_Load(Object Src, EventArgs E) { static int Counter = 0; } The question is, will this static variable become global? mean everyone login to the page from different session see the same value? or the value only applicable for current session? Thanks. :) regards, vic

    D H 2 Replies Last reply
    0
    • G goatstudio

      I define some of my variables in asp.net page as a static variable, to keep track of the value when do a postback. For exmaple protected void Page_Load(Object Src, EventArgs E) { static int Counter = 0; } The question is, will this static variable become global? mean everyone login to the page from different session see the same value? or the value only applicable for current session? Thanks. :) regards, vic

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Your question would be better handled in the ASP.NET Forum. This is a web development/ASP.NET question, regardless of the language that the code-behind is written in. The static variable idea won't work considering that ASP.NET, as any HTML page, is stateless. You have to save the values to hidden fields on the page, to be read back when the page is posted back to the server, or in the Session object, or somewhere else that can handle state. And, no, it won't become global. There is no such thing in the .NET Framework. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

      G 1 Reply Last reply
      0
      • D Dave Kreskowiak

        Your question would be better handled in the ASP.NET Forum. This is a web development/ASP.NET question, regardless of the language that the code-behind is written in. The static variable idea won't work considering that ASP.NET, as any HTML page, is stateless. You have to save the values to hidden fields on the page, to be read back when the page is posted back to the server, or in the Session object, or somewhere else that can handle state. And, no, it won't become global. There is no such thing in the .NET Framework. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

        G Offline
        G Offline
        goatstudio
        wrote on last edited by
        #3

        Dave Kreskowiak, Thanks for the info. :) My static variable actually work on the page... just that I am worry it become global. Your answer make me feel better. :-D p/s: I post it here coz I am using C# in my asp.net. Anyway thanks for reminding me. ;P

        1 Reply Last reply
        0
        • G goatstudio

          I define some of my variables in asp.net page as a static variable, to keep track of the value when do a postback. For exmaple protected void Page_Load(Object Src, EventArgs E) { static int Counter = 0; } The question is, will this static variable become global? mean everyone login to the page from different session see the same value? or the value only applicable for current session? Thanks. :) regards, vic

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          While I normally agree with Dave, I don't see this as specific to ASP.NET so I'll bite. This won't even compile. You cannot declare fields (a static int Counter looks like a field to the compiler) inside method implementations. If you need to declare a static variable, do so in the class (your derivative page) like so:

          static int Counter = 0; // private
           
          // Override On_EventName_ when possible - it's faster with more control
          protected override void OnLoad(EventArgs e)
          {
          Counter++;
          base.OnLoad();
          }

          This will increment for the lifetime of the object, which is alive for as long as the AppDomain is alive in which it was created. For Windows Forms applications, this is typically as long as your application is running (your application could, however, create a new AppDomain). With ASP.NET, this is for as long as the web application doesn't need restarting (like after editing the Web.config file for the site or application; or, again, unless you created a separate AppDomain for some reason). This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]

          G D 2 Replies Last reply
          0
          • H Heath Stewart

            While I normally agree with Dave, I don't see this as specific to ASP.NET so I'll bite. This won't even compile. You cannot declare fields (a static int Counter looks like a field to the compiler) inside method implementations. If you need to declare a static variable, do so in the class (your derivative page) like so:

            static int Counter = 0; // private
             
            // Override On_EventName_ when possible - it's faster with more control
            protected override void OnLoad(EventArgs e)
            {
            Counter++;
            base.OnLoad();
            }

            This will increment for the lifetime of the object, which is alive for as long as the AppDomain is alive in which it was created. For Windows Forms applications, this is typically as long as your application is running (your application could, however, create a new AppDomain). With ASP.NET, this is for as long as the web application doesn't need restarting (like after editing the Web.config file for the site or application; or, again, unless you created a separate AppDomain for some reason). This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]

            G Offline
            G Offline
            goatstudio
            wrote on last edited by
            #5

            Heath Stewart, Thanks for the explaination! :-D You are right, I actually declare the static variable outside the Page_Load method. My mistake here... ;P I just want to make sure that, as long as the static variable won't share by other sessions in the same server... it will be fine... ;) Thanks guy... ;)

            H 1 Reply Last reply
            0
            • G goatstudio

              Heath Stewart, Thanks for the explaination! :-D You are right, I actually declare the static variable outside the Page_Load method. My mistake here... ;P I just want to make sure that, as long as the static variable won't share by other sessions in the same server... it will be fine... ;) Thanks guy... ;)

              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #6

              Statics are always within the context of an AppDomain, so if your process consists of two or more AppDomains statics will be different. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]

              1 Reply Last reply
              0
              • H Heath Stewart

                While I normally agree with Dave, I don't see this as specific to ASP.NET so I'll bite. This won't even compile. You cannot declare fields (a static int Counter looks like a field to the compiler) inside method implementations. If you need to declare a static variable, do so in the class (your derivative page) like so:

                static int Counter = 0; // private
                 
                // Override On_EventName_ when possible - it's faster with more control
                protected override void OnLoad(EventArgs e)
                {
                Counter++;
                base.OnLoad();
                }

                This will increment for the lifetime of the object, which is alive for as long as the AppDomain is alive in which it was created. For Windows Forms applications, this is typically as long as your application is running (your application could, however, create a new AppDomain). With ASP.NET, this is for as long as the web application doesn't need restarting (like after editing the Web.config file for the site or application; or, again, unless you created a separate AppDomain for some reason). This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #7

                Hey Heath! How's life in the shadow of an actively rumbling volcano? ;) I thought he was asking if it was possible to persist a session-specific value using a static, like you've suggested. I did't think that's possible, but I could be wrong? Is the AppDomain session-scoped or application-scoped? Seems like a dumb question, I know. But just for clarification purposes... Also, just to point out for future readers ... yeah, right! ;) ... using a Static such as this, in either Session or Application scope, does not propogate it's value acrossed multiple servers hosting the same web application. So, if you have two or more web servers running the same application, say for load balancing purposes, all servers will have different values for the Static "global" variable. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                H 1 Reply Last reply
                0
                • D Dave Kreskowiak

                  Hey Heath! How's life in the shadow of an actively rumbling volcano? ;) I thought he was asking if it was possible to persist a session-specific value using a static, like you've suggested. I did't think that's possible, but I could be wrong? Is the AppDomain session-scoped or application-scoped? Seems like a dumb question, I know. But just for clarification purposes... Also, just to point out for future readers ... yeah, right! ;) ... using a Static such as this, in either Session or Application scope, does not propogate it's value acrossed multiple servers hosting the same web application. So, if you have two or more web servers running the same application, say for load balancing purposes, all servers will have different values for the Static "global" variable. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                  H Offline
                  H Offline
                  Heath Stewart
                  wrote on last edited by
                  #8

                  Dave Kreskowiak wrote: How's life in the shadow of an actively rumbling volcano? Exciting! I've been through tornados having been from the midwest, but this is a new experience. I had a lot of interest in volcanoes (and earthquakes) when I was younger (did a fair share more than just "class projects" on them) so this will be fun to see in person. If I get a chance I'll be taking some pics. :) It's not expected to be dangerous to the greater Puget Sound area, though, so I'm not worried. What is exciting is that I've killed two potentially deadly spiders in our new apartment, all within a week of moving in! While they look like brown recluse spiders, 1) those are only supposed to be in the south-east, and 2) they're often mistaken for hobo spiders, which are in this area (and just as potentially deadly). Back to the topic at hand... As far as the problem goes, it really all comes down to AppDomains. A process can have more than one AppDomain, and a web application is an AppDomain. As long as the AppDomain is active, the statics are, well, static. Since an AppDomain cannot span multiple machines, what you said about services is true. To the original poster, if you want a "global" variable across multiple server or applications, you either need to persist it in some storage medium (like a database, although that can be pretty slow depending on load) or use .NET Remoting which allows communication between multiple AppDomains (through indepedent transport and formatting channels). You could, for example, have a singleton running on some machine and the separate servers communicate with the remoting object through a proxy (which you implement a mutex to lock the increment and/or decrement). There's lots of different ways to solving the problem. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]

                  D 1 Reply Last reply
                  0
                  • H Heath Stewart

                    Dave Kreskowiak wrote: How's life in the shadow of an actively rumbling volcano? Exciting! I've been through tornados having been from the midwest, but this is a new experience. I had a lot of interest in volcanoes (and earthquakes) when I was younger (did a fair share more than just "class projects" on them) so this will be fun to see in person. If I get a chance I'll be taking some pics. :) It's not expected to be dangerous to the greater Puget Sound area, though, so I'm not worried. What is exciting is that I've killed two potentially deadly spiders in our new apartment, all within a week of moving in! While they look like brown recluse spiders, 1) those are only supposed to be in the south-east, and 2) they're often mistaken for hobo spiders, which are in this area (and just as potentially deadly). Back to the topic at hand... As far as the problem goes, it really all comes down to AppDomains. A process can have more than one AppDomain, and a web application is an AppDomain. As long as the AppDomain is active, the statics are, well, static. Since an AppDomain cannot span multiple machines, what you said about services is true. To the original poster, if you want a "global" variable across multiple server or applications, you either need to persist it in some storage medium (like a database, although that can be pretty slow depending on load) or use .NET Remoting which allows communication between multiple AppDomains (through indepedent transport and formatting channels). You could, for example, have a singleton running on some machine and the separate servers communicate with the remoting object through a proxy (which you implement a mutex to lock the increment and/or decrement). There's lots of different ways to solving the problem. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]

                    D Offline
                    D Offline
                    Dave Kreskowiak
                    wrote on last edited by
                    #9

                    Heath Stewart wrote: Exciting! I've been through tornados having been from the midwest, but this is a new experience. I had a lot of interest in volcanoes (and earthquakes) when I was younger (did a fair share more than just "class projects" on them) so this will be fun to see in person. If I get a chance I'll be taking some pics. Awsome! :-D I'm so jealous! I've had that same, call it morbid, curiosity about how these things work and still have it to this day. I've been within 500 feet of a tornado before, BUT COULDN'T SEE IT! because it was heavily rain-wrapped. All we saw, looking through the 8 foot tall plate glass windows in the front of the store ;P, were roofs being torn off buildings ... what a let-down that was. I've even had the pleasure of watching empty Coke cans slide back and forth acrossed my desk during earthquakes in and around South-East Michigan. I know, it's not an earthquake mecca and they're weak by Pacific Coast standards, but it's still pretty funny to try and balance on one foot while putting jeans on during an earthquake (1986 - epicenter in middle of Lake Erie). If Mt St Helens blows again, we want pictures! LOTS of pictures! Setup a webcam if you have to! Who knows if the one at the Johnston Ridge Observatory[^] will be working if/when it does decide to erupt. and now, back to our show... --------8<-------------------------- I agree with your assessment of the speed of persisting Session info to an SQL server. It's as flexible a solution as your going to get, but it does tend to be a little slow. I've installed the SQL state server package that comes with, I can't remember which or where, but I think, ASP.NET or Visual Studio .NET Enterprise. I've thought about both single and multiple server session state caching schemes. A single server solution is easy to implement, but has the downfall of not being scalable to support say, 15 IIS servers. Mostly, I've wondered what the benefits, limitations and pitfalls are of a solution consisting of small cluster of servers, no more than 3 to start, each running a "Singleton" component, but sync'ing up their data on a real-time basis. These servers could possibly run behind a single-IP solution to facilitate fault-tolerance. The list of ponderings I've come up with is pretty long... What would be the database s

                    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