Back in the "old" C/C++ days, I used to declare static variables inside
functions. Something like...
// just a silly example to demonstrate the technique
int foo(void)
{
static int NextVal = 0;
NextVal = (++NextVal) % 317;
return (NextVal);
}
I use this technique/style for many varied things but basically this gave me
a quick and easy to maintain a value between function calls while limiting
the visibility of the variable.
I'm struggling to find a C# equivalent technique. The best equivalents I've
discovered so far:
1) If foo is a member function of class fred, create a private variable in
fred on which foo operates. This is *OK* but gives the variable whole class
scope when only one function NEEDS it. It would also seem to "clutter" the
class namespace unnecessarily.
2) Create a small foo class with just a get method. NextVal would be a
private variable inside the small foo class. This is also *OK* but setting
up a separate class just to appropriately scope a variable and maintain it
between calls seems overkill. I understand writing the class is not a huge
deal, but I'll be creating lots of tiny classes and it will be yet another
paradigm shift.
Is there a third option or a better way to accomplish the "limit scope" and
"maintain value between calls" functionality of my old static technique?
Any help/suggestions are greatly appreciated.
An old dog trying to learn new tricks,
John Kelsey