static variable
-
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
-
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
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
-
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
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.
-
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.
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
-
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
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
-
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
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
-
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