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++tutorial
21 Posts 8 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.
  • T Offline
    T Offline
    tomiga
    wrote on last edited by
    #1

    Is it possible to define something like variables in methods which have static modifier: C++ example: void MyFun() { static int i = 0; // after next call MyFun() i = 1 i++; }

    J 1 Reply Last reply
    0
    • T tomiga

      Is it possible to define something like variables in methods which have static modifier: C++ example: void MyFun() { static int i = 0; // after next call MyFun() i = 1 i++; }

      J Offline
      J Offline
      James T Johnson
      wrote on last edited by
      #2

      No it doesn't, static can only be applied to Methods, Properties, Fields, Operators, and Constructors. James Simplicity Rules!

      T 1 Reply Last reply
      0
      • J James T Johnson

        No it doesn't, static can only be applied to Methods, Properties, Fields, Operators, and Constructors. James Simplicity Rules!

        T Offline
        T Offline
        Tom Archer
        wrote on last edited by
        #3

        Don't you find that omission completely indefensable? Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af

        N J E 3 Replies Last reply
        0
        • T Tom Archer

          Don't you find that omission completely indefensable? Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af

          N Offline
          N Offline
          Nish Nishant
          wrote on last edited by
          #4

          What purpose would be served by making a local scope variable static? Nish


          The posting stats are now in PDF:- http://www.busterboy.org/codeproject/ Feel free to make your comments. Updated - May 04th, Saturday

          T T 2 Replies Last reply
          0
          • N Nish Nishant

            What purpose would be served by making a local scope variable static? Nish


            The posting stats are now in PDF:- http://www.busterboy.org/codeproject/ Feel free to make your comments. Updated - May 04th, Saturday

            T Offline
            T Offline
            Tom Archer
            wrote on last edited by
            #5

            Helps when writing recursive functions Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af

            N D 2 Replies Last reply
            0
            • T Tom Archer

              Helps when writing recursive functions Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af

              N Offline
              N Offline
              Nish Nishant
              wrote on last edited by
              #6

              Tom Archer wrote: Helps when writing recursive functions Oh! Can you further enlighten a confused newbie? Nish


              The posting stats are now in PDF:- http://www.busterboy.org/codeproject/ Feel free to make your comments. Updated - May 04th, Saturday

              T 1 Reply Last reply
              0
              • N Nish Nishant

                Tom Archer wrote: Helps when writing recursive functions Oh! Can you further enlighten a confused newbie? Nish


                The posting stats are now in PDF:- http://www.busterboy.org/codeproject/ Feel free to make your comments. Updated - May 04th, Saturday

                T Offline
                T Offline
                Tom Archer
                wrote on last edited by
                #7

                On what a recursive function is or when you'd use one or ...? Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af

                N N 2 Replies Last reply
                0
                • T Tom Archer

                  On what a recursive function is or when you'd use one or ...? Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af

                  N Offline
                  N Offline
                  Nish Nishant
                  wrote on last edited by
                  #8

                  Tom Archer wrote: On what a recursive function is or when you'd use one or ...? Blast!!! Not that!!!! I meant, I wanted to know how a static variable was going to be useful in a recursive function!!! Nish!!!!


                  The posting stats are now in PDF:- http://www.busterboy.org/codeproject/ Feel free to make your comments. Updated - May 04th, Saturday

                  T 1 Reply Last reply
                  0
                  • N Nish Nishant

                    Tom Archer wrote: On what a recursive function is or when you'd use one or ...? Blast!!! Not that!!!! I meant, I wanted to know how a static variable was going to be useful in a recursive function!!! Nish!!!!


                    The posting stats are now in PDF:- http://www.busterboy.org/codeproject/ Feel free to make your comments. Updated - May 04th, Saturday

                    T Offline
                    T Offline
                    Tom Archer
                    wrote on last edited by
                    #9

                    That's a lot of excitement Nish. Are you sure you're taking your Lithium like you're supposed to :) If you write a function that can call itself (a recursive function) one way to maintain state information is via static variables because they won't reinitilaize in the function's prolog. Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af

                    N 1 Reply Last reply
                    0
                    • T Tom Archer

                      That's a lot of excitement Nish. Are you sure you're taking your Lithium like you're supposed to :) If you write a function that can call itself (a recursive function) one way to maintain state information is via static variables because they won't reinitilaize in the function's prolog. Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af

                      N Offline
                      N Offline
                      Nish Nishant
                      wrote on last edited by
                      #10

                      Tom Archer wrote: Are you sure you're taking your Lithium like you're supposed to Gosh! Not that old AI bot thing again :-) Tom Archer wrote: If you write a function that can call itself (a recursive function) one way to maintain state information is via static variables because they won't reinitilaize in the function's prolog. I thought of that actually. But I thought since it was Tom Archer saying it, there ought to be a more complicated explantion to it :-) Didn;'t think that the obvious one would be the only one. Nish


                      The posting stats are now in PDF:- http://www.busterboy.org/codeproject/ Feel free to make your comments. Updated - May 04th, Saturday

                      T 1 Reply Last reply
                      0
                      • N Nish Nishant

                        Tom Archer wrote: Are you sure you're taking your Lithium like you're supposed to Gosh! Not that old AI bot thing again :-) Tom Archer wrote: If you write a function that can call itself (a recursive function) one way to maintain state information is via static variables because they won't reinitilaize in the function's prolog. I thought of that actually. But I thought since it was Tom Archer saying it, there ought to be a more complicated explantion to it :-) Didn;'t think that the obvious one would be the only one. Nish


                        The posting stats are now in PDF:- http://www.busterboy.org/codeproject/ Feel free to make your comments. Updated - May 04th, Saturday

                        T Offline
                        T Offline
                        Tom Archer
                        wrote on last edited by
                        #11

                        Sometimes even I can't over-complicate something :) Seriously, maintaining state information across call boundaries is the main reason for using static variables within a method. Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af

                        1 Reply Last reply
                        0
                        • T Tom Archer

                          Don't you find that omission completely indefensable? Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af

                          J Offline
                          J Offline
                          James T Johnson
                          wrote on last edited by
                          #12

                          Tom Archer wrote: Don't you find that omission completely indefensable? Yes, while you can fake it by putting static variables in the class; it doesn't do anything for variables you would like to keep localized only to the method. Maybe we can see it in v.next? :) James Simplicity Rules!

                          N 1 Reply Last reply
                          0
                          • J James T Johnson

                            Tom Archer wrote: Don't you find that omission completely indefensable? Yes, while you can fake it by putting static variables in the class; it doesn't do anything for variables you would like to keep localized only to the method. Maybe we can see it in v.next? :) James Simplicity Rules!

                            N Offline
                            N Offline
                            Neil Van Note
                            wrote on last edited by
                            #13

                            In C++, static locals are shared among all instances of a class, not very useful for state information of a recursive function. I don't know if a C# incarnation would follow the same pattern. In C++ I have never found a good reason to use the feature unless it was for returning the reference to a static object that required more aggressive initialization than just plain assignment or to defer potentially expensive initialization of such an object until it was actually used (which could be never). Regards

                            J T 2 Replies Last reply
                            0
                            • N Neil Van Note

                              In C++, static locals are shared among all instances of a class, not very useful for state information of a recursive function. I don't know if a C# incarnation would follow the same pattern. In C++ I have never found a good reason to use the feature unless it was for returning the reference to a static object that required more aggressive initialization than just plain assignment or to defer potentially expensive initialization of such an object until it was actually used (which could be never). Regards

                              J Offline
                              J Offline
                              James T Johnson
                              wrote on last edited by
                              #14

                              Neil Van Note wrote: In C++, static locals are shared among all instances of a class, not very useful for state information of a recursive function. Doh! You're right here; guess a regular class-level variable would be the solution ;P James Simplicity Rules!

                              1 Reply Last reply
                              0
                              • T Tom Archer

                                On what a recursive function is or when you'd use one or ...? Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af

                                N Offline
                                N Offline
                                Nick Parker
                                wrote on last edited by
                                #15

                                Tom Archer wrote: On what a recursive function is or when you'd use one or ...? I think this is Tom trying to be funny. :-D Nick Parker

                                1 Reply Last reply
                                0
                                • N Neil Van Note

                                  In C++, static locals are shared among all instances of a class, not very useful for state information of a recursive function. I don't know if a C# incarnation would follow the same pattern. In C++ I have never found a good reason to use the feature unless it was for returning the reference to a static object that required more aggressive initialization than just plain assignment or to defer potentially expensive initialization of such an object until it was actually used (which could be never). Regards

                                  T Offline
                                  T Offline
                                  Tom Archer
                                  wrote on last edited by
                                  #16

                                  Neil Van Note wrote: In C++, static locals are shared among all instances of a class, not very useful for state information of a recursive function. I disagree. You're making the assumption that the class will be instantiated many times. Neil Van Note wrote: In C++ I have never found a good reason to use the feature One example is how window procs were/are written without MFC. Another example if when iterating over a chain of entities where each function call works on a given link and a static variable is used to keep track of current position. Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af

                                  N 1 Reply Last reply
                                  0
                                  • T Tom Archer

                                    Neil Van Note wrote: In C++, static locals are shared among all instances of a class, not very useful for state information of a recursive function. I disagree. You're making the assumption that the class will be instantiated many times. Neil Van Note wrote: In C++ I have never found a good reason to use the feature One example is how window procs were/are written without MFC. Another example if when iterating over a chain of entities where each function call works on a given link and a static variable is used to keep track of current position. Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af

                                    N Offline
                                    N Offline
                                    Neil Van Note
                                    wrote on last edited by
                                    #17

                                    Tom Archer wrote: I disagree. You're making the assumption that the class will be instantiated many times. In this case, I am. And you are assuming the class is a singleton, this was not noted in the discussion, and I have to assume that it is not and point the fact out. I code very defensively. Tom Archer wrote: One example is how window procs were/are written without MFC What does a local static have to do with the implementation of a class borne window procedure? This can be achieved very easily without it, and prove to be much more reusable. Tom Archer wrote: Another example if when iterating over a chain of entities where each function call works on a given link and a static variable is used to keep track of current position I cannot picture a situation where it would be used this way and prove more elegant than other straight forward solutions. Do you have an example? Regards

                                    1 Reply Last reply
                                    0
                                    • N Nish Nishant

                                      What purpose would be served by making a local scope variable static? Nish


                                      The posting stats are now in PDF:- http://www.busterboy.org/codeproject/ Feel free to make your comments. Updated - May 04th, Saturday

                                      T Offline
                                      T Offline
                                      tomiga
                                      wrote on last edited by
                                      #18

                                      Static variables sometimes are very usefull. You can use it or not... but in my opinion programmer should decided what he wants to use (in this case simply language dosn't support it - that's a pity) Tomiga

                                      N 1 Reply Last reply
                                      0
                                      • T tomiga

                                        Static variables sometimes are very usefull. You can use it or not... but in my opinion programmer should decided what he wants to use (in this case simply language dosn't support it - that's a pity) Tomiga

                                        N Offline
                                        N Offline
                                        Neil Van Note
                                        wrote on last edited by
                                        #19

                                        The language supports class scope static variables; I don't see any reason why it should go any further. I have yet to see an instance, from an OOP standpoint, where it would beneficial to do otherwise. Regards

                                        1 Reply Last reply
                                        0
                                        • T Tom Archer

                                          Don't you find that omission completely indefensable? Cheers, Tom Archer Author, Inside C# Author, Visual C++.NET Bible A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af

                                          E Offline
                                          E Offline
                                          Eric Gunnerson msft
                                          wrote on last edited by
                                          #20

                                          IIRC, there are two reasons we didn't do it: 1) You don't get much additional utility over an instance or static variable. 2) They tend to cause subtle problems when you start dealing with multi-threaded use. They're also harder to find when you're looking for such issues.

                                          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