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 variables in methods

Static variables in methods

Scheduled Pinned Locked Moved C#
c++tutorialquestion
6 Posts 5 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.
  • A Offline
    A Offline
    Amirjalaly
    wrote on last edited by
    #1

    How can I define a static variable in a method for example like this: private method() { static int c=0; c++; }

    A J W 3 Replies Last reply
    0
    • A Amirjalaly

      How can I define a static variable in a method for example like this: private method() { static int c=0; c++; }

      A Offline
      A Offline
      Alvaro Mendez
      wrote on last edited by
      #2

      You can't; it's not allowed in C#. (Anyone know why?) The workaround is to declare your static variable as a field. static int c=0; private method() { c++; } Regards, Alvaro


      He who laughs last, thinks slowest.

      J 1 Reply Last reply
      0
      • A Amirjalaly

        How can I define a static variable in a method for example like this: private method() { static int c=0; c++; }

        J Offline
        J Offline
        Jeff Varszegi
        wrote on last edited by
        #3

        You can't. Static variables belong to an entire class, not a method. To define a variable inside a static method, just leave out the word 'static'. I remember programming in LotusScript, an old VB-based language. You could define a static function, and any variable declared in the function would be kept around for the next invocation of the function. C# doesn't work like that. Regards, Jeff Varszegi

        1 Reply Last reply
        0
        • A Amirjalaly

          How can I define a static variable in a method for example like this: private method() { static int c=0; c++; }

          W Offline
          W Offline
          Wernand
          wrote on last edited by
          #4

          you must define the static as a field, now you get Compiler Error CS1525. Try this: static int c = 0; private void method() { c++; } Regards, Wernand.

          1 Reply Last reply
          0
          • A Alvaro Mendez

            You can't; it's not allowed in C#. (Anyone know why?) The workaround is to declare your static variable as a field. static int c=0; private method() { c++; } Regards, Alvaro


            He who laughs last, thinks slowest.

            J Offline
            J Offline
            jparsons
            wrote on last edited by
            #5

            IMHO it was taken out because it was a dumb idea and easily screwed up Jared jparsons@jparsons.org www.prism.gatech.edu/~gte477n

            A 1 Reply Last reply
            0
            • J jparsons

              IMHO it was taken out because it was a dumb idea and easily screwed up Jared jparsons@jparsons.org www.prism.gatech.edu/~gte477n

              A Offline
              A Offline
              Alvaro Mendez
              wrote on last edited by
              #6

              Hmmm, the strange thing is that it's also not allowed in Java. I wonder if there's a correlation... jparsons wrote: it was a dumb idea and easily screwed up I don't consider it a dumb idea. There are times when it's nice to hold on to a value that only applies to a specific method. The problem may lie in the fact that static variables are initialized only once, so they can cause confusion:

              void foo()
              {
              static int n = 0;
              ...
              }

              When you see a method like that in C++, you need to keep in mind that the statement is only executed the first time the method is called. The casual (or careless) observer may not be aware of that, and erroneously think the method executes every time -- I'm pretty sure it's happened to me before :-O. Regards, Alvaro


              He who laughs last, thinks slowest.

              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