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#
c++csharptutorialquestion
7 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.
  • G Offline
    G Offline
    gekoscan
    wrote on last edited by
    #1

    In Visual C++ I could declare a static inside a function and no matter how many times this function was called it would only be called once. ie. public void foo1() { static int iCount = 0; iCount++; Console.WriteLine(iCount); } public void foo2() { while(true) foo1(); } This example should just continue to increment a value in the console because the initialization is only done once. How is this done in C#? Thanks

    R 1 Reply Last reply
    0
    • G gekoscan

      In Visual C++ I could declare a static inside a function and no matter how many times this function was called it would only be called once. ie. public void foo1() { static int iCount = 0; iCount++; Console.WriteLine(iCount); } public void foo2() { while(true) foo1(); } This example should just continue to increment a value in the console because the initialization is only done once. How is this done in C#? Thanks

      R Offline
      R Offline
      Rocky Moore
      wrote on last edited by
      #2

      IBF EHT KCAF wrote: This example should just continue to increment a value in the console because the initialization is only done once. How is this done in C#? It wouldn't be allowed. Static data would have to live in the class and not the methods. Also, Static anything has to have a full referrence to it. As an example:

      class myOwn
      {
      	static int myStaticStore = 0;
      
      	public int myMember(int StaticStore)
      	{
      		myOwn.myStaticStore=StaticStore;
      		return myOwn.myStaticStore;
      	}
      }
      

      Rocky Moore

      G 1 Reply Last reply
      0
      • R Rocky Moore

        IBF EHT KCAF wrote: This example should just continue to increment a value in the console because the initialization is only done once. How is this done in C#? It wouldn't be allowed. Static data would have to live in the class and not the methods. Also, Static anything has to have a full referrence to it. As an example:

        class myOwn
        {
        	static int myStaticStore = 0;
        
        	public int myMember(int StaticStore)
        	{
        		myOwn.myStaticStore=StaticStore;
        		return myOwn.myStaticStore;
        	}
        }
        

        Rocky Moore

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

        Nope see in C++ this is totally allowed and it makes recurrsion really nice and easy. If I want to write a recursive function I want to be able to have variables inside the method that all the instances of that method can see. I don't want to have 4 variables declared globally inside a class when only 1 method is using them. I want to declare them static inside a method so only 1 instance exists for all instances of that method. If this isn't somehow possible in C# then this language has been totally *ucked for recurrsion (GOOD JOB Microsoft). You just took some real horsepower out of your own language.

        R 1 Reply Last reply
        0
        • G gekoscan

          Nope see in C++ this is totally allowed and it makes recurrsion really nice and easy. If I want to write a recursive function I want to be able to have variables inside the method that all the instances of that method can see. I don't want to have 4 variables declared globally inside a class when only 1 method is using them. I want to declare them static inside a method so only 1 instance exists for all instances of that method. If this isn't somehow possible in C# then this language has been totally *ucked for recurrsion (GOOD JOB Microsoft). You just took some real horsepower out of your own language.

          R Offline
          R Offline
          Rocky Moore
          wrote on last edited by
          #4

          bytewave wrote: If this isn't somehow possible in C# then this language has been totally *ucked for recurrsion (GOOD JOB Microsoft). You just took some real horsepower out of your own language It has the same functionality, only the scope of the variable is global to the class rather than per member. You will still only have _one_ varible, for all instances of the class just like in C++. The only time you would have a concern is if you planned to use a static variable of the same name in multiple methods as you could do in C++. Since they are up at the class level they would have to have different names. Rocky Moore

          J 1 Reply Last reply
          0
          • R Rocky Moore

            bytewave wrote: If this isn't somehow possible in C# then this language has been totally *ucked for recurrsion (GOOD JOB Microsoft). You just took some real horsepower out of your own language It has the same functionality, only the scope of the variable is global to the class rather than per member. You will still only have _one_ varible, for all instances of the class just like in C++. The only time you would have a concern is if you planned to use a static variable of the same name in multiple methods as you could do in C++. Since they are up at the class level they would have to have different names. Rocky Moore

            J Offline
            J Offline
            Jonathan Tan
            wrote on last edited by
            #5

            Well...VB6 also has a "static" modifier that works the same way as C++...i don't know if VB.NET still has it...but it should be quite simple to implement in C# and yet Microsoft doesn't do it...another one is the With block which is in VB but not in C#...how irritating

            R 1 Reply Last reply
            0
            • J Jonathan Tan

              Well...VB6 also has a "static" modifier that works the same way as C++...i don't know if VB.NET still has it...but it should be quite simple to implement in C# and yet Microsoft doesn't do it...another one is the With block which is in VB but not in C#...how irritating

              R Offline
              R Offline
              Rocky Moore
              wrote on last edited by
              #6

              Jonathan Tan wrote: another one is the With block which is in VB but not in C#...how irritating I agree with that. Would have been nice. I am just glad they put in "Using" to shorten all those referrence calls! Rocky Moore

              A 1 Reply Last reply
              0
              • R Rocky Moore

                Jonathan Tan wrote: another one is the With block which is in VB but not in C#...how irritating I agree with that. Would have been nice. I am just glad they put in "Using" to shorten all those referrence calls! Rocky Moore

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

                I don't really care either way, but the with statement is addressed here. Andy Gaskell, MCSD MCDBA

                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