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. persistant variable?

persistant variable?

Scheduled Pinned Locked Moved C#
question
7 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.
  • A Offline
    A Offline
    amatbrewer
    wrote on last edited by
    #1

    Is it possable to make a variable in a function persistant without declairing it outside of the function? That is, retain its value between calls to that function while still allowing it to be changed. e.g. private void MyFunction(string sSillyGarbage) { //unessaraly complex code int iMyPersistantVariable; //tons of code that could probably be accomplished in // 2 lines if i wasn't such a hack. } Thanks,

    David Wilkes

    G L 2 Replies Last reply
    0
    • A amatbrewer

      Is it possable to make a variable in a function persistant without declairing it outside of the function? That is, retain its value between calls to that function while still allowing it to be changed. e.g. private void MyFunction(string sSillyGarbage) { //unessaraly complex code int iMyPersistantVariable; //tons of code that could probably be accomplished in // 2 lines if i wasn't such a hack. } Thanks,

      David Wilkes

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      amatbrewer wrote:

      Is it possable to make a variable in a function persistant without declairing it outside of the function?

      No, that is not possible. The local variables in a method are allocated on the stack when the method is called, and deallocated when it returns.

      --- It's amazing to see how much work some people will go through just to avoid a little bit of work.

      A 1 Reply Last reply
      0
      • A amatbrewer

        Is it possable to make a variable in a function persistant without declairing it outside of the function? That is, retain its value between calls to that function while still allowing it to be changed. e.g. private void MyFunction(string sSillyGarbage) { //unessaraly complex code int iMyPersistantVariable; //tons of code that could probably be accomplished in // 2 lines if i wasn't such a hack. } Thanks,

        David Wilkes

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        The general answer is no: C# has no equivalent to the "static" keyword applicable to local variables in a function (but C and C++ do). There is one exception (starting .NET 2.0): within an iterator, a function can "yield return" which means its locals remain alive and keep their value. See MSDN for more info and examples. :)

        Luc Pattyn

        G 1 Reply Last reply
        0
        • L Luc Pattyn

          The general answer is no: C# has no equivalent to the "static" keyword applicable to local variables in a function (but C and C++ do). There is one exception (starting .NET 2.0): within an iterator, a function can "yield return" which means its locals remain alive and keep their value. See MSDN for more info and examples. :)

          Luc Pattyn

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          Luc Pattyn wrote:

          C# has no equivalent to the "static" keyword applicable to local variables in a function (but C and C++ do).

          Which I consider to be a wise decision. :) Declaring a static variable in a method means that it's created at class level, not as a local variable. I find it more logical to declare it where it is created. Also, if a method could have static "local" variables, there should also be an "instance" keyword so that you could declare non-static class level "local" variables.

          --- It's amazing to see how much work some people will go through just to avoid a little bit of work.

          L 1 Reply Last reply
          0
          • G Guffa

            Luc Pattyn wrote:

            C# has no equivalent to the "static" keyword applicable to local variables in a function (but C and C++ do).

            Which I consider to be a wise decision. :) Declaring a static variable in a method means that it's created at class level, not as a local variable. I find it more logical to declare it where it is created. Also, if a method could have static "local" variables, there should also be an "instance" keyword so that you could declare non-static class level "local" variables.

            --- It's amazing to see how much work some people will go through just to avoid a little bit of work.

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            I beg to differ, yes the compiler/linker will allocate a function-local static variable outside the function (to keep it alive when the function returns), but its scope is limited to the function itself, making it inaccessible to other functions (encapsulation), which is not what you get by declaring a (static or other) variable outside the function. So it has a purpose, and I would not mind C# offering the same possibilities.

            Luc Pattyn

            G 1 Reply Last reply
            0
            • L Luc Pattyn

              I beg to differ, yes the compiler/linker will allocate a function-local static variable outside the function (to keep it alive when the function returns), but its scope is limited to the function itself, making it inaccessible to other functions (encapsulation), which is not what you get by declaring a (static or other) variable outside the function. So it has a purpose, and I would not mind C# offering the same possibilities.

              Luc Pattyn

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              Luc Pattyn wrote:

              I beg to differ, yes the compiler/linker will allocate a function-local static variable outside the function (to keep it alive when the function returns), but its scope is limited to the function itself, making it inaccessible to other functions (encapsulation), which is not what you get by declaring a (static or other) variable outside the function.

              That is exactly what I think is the problem. Normally the scope of a variable is consistent with where it's stored, but allowing "local" static variables creates a variable where the scope differs substantially from where it's stored. This is not encapsulation. That is done on class level, not inside a class.

              --- It's amazing to see how much work some people will go through just to avoid a little bit of work.

              1 Reply Last reply
              0
              • G Guffa

                amatbrewer wrote:

                Is it possable to make a variable in a function persistant without declairing it outside of the function?

                No, that is not possible. The local variables in a method are allocated on the stack when the method is called, and deallocated when it returns.

                --- It's amazing to see how much work some people will go through just to avoid a little bit of work.

                A Offline
                A Offline
                amatbrewer
                wrote on last edited by
                #7

                Guffa wrote:

                It's amazing to see how much work some people will go through just to avoid a little bit of work.

                "Its hard work, being this lazy!" :laugh:

                David Wilkes

                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