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