C# Equivalent to C/C++ static variable in a function?

J

John Kelsey

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
 
C

Colin Neller

John,

I too have missed local static variables. (1) is (class members) is the
best solution I've found. (2) seems like over-kill to me. I guess a third
option would be using VB.NET, but since it may be considered treason to
mention such a thing in a C# news group, pretend you didn't just read that
:)
 
J

Joanna Carter [TeamB]

"John Kelsey" <[email protected]> a écrit dans le message de (e-mail address removed)...

| Back in the "old" C/C++ days, I used to declare static variables inside
| functions. Something like...

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

This is the "normal" way to do it; this way the variable has the same life
as the instance. It is also semantically correct because methods should not
have persistent state, to avoid threading problems; etc.

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

This will not make any difference because you will have to have a private
instance of the foo class in your class, just as with the straight variable,
this will be accessible to all methods of the class. Of course a static
class method could have even greater scope.

| 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?

Not AFAIK, see
http://blogs.msdn.com/csharpfaq/archive/2004/05/11/130248.aspx

Using a private field really is the "normal" way :)

Joanna
 
G

Guest

Static local variables are also available in C++/CLI by the way.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter
 
J

John Kelsey

Joanna Carter said:
"John Kelsey" <[email protected]> a écrit dans le message de (e-mail address removed)...
--snip--
| 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.

This will not make any difference because you will have to have a private
instance of the foo class in your class, just as with the straight
variable,
this will be accessible to all methods of the class. Of course a static
class method could have even greater scope.
foo, the function or the class can have public or private, appropriate for
what needs it. I just don't want anything accessing the variable NextVal in
the containing class fred except foo. The only way I understand to do that
in C# is to create a private variable inside a tiny class containing only a
public get method.

My first mentor insisted on appropriate data scoping. If foo is the only
thing that should see NextVal, it must be the only thing that can see
NextVal. In C#, so far this seems to require a mini-class.

Thanks for the help,
John Kelsey
 
J

Joanna Carter [TeamB]

"John Kelsey" <[email protected]> a écrit dans le message de u%[email protected]...

| foo, the function or the class can have public or private, appropriate for
| what needs it. I just don't want anything accessing the variable NextVal
in
| the containing class fred except foo. The only way I understand to do
that
| in C# is to create a private variable inside a tiny class containing only
a
| public get method.

All that means is that you then have to have an instance of that tiny class
in order to get at the public get method. Since you can't hide the class
declaration inside the method foo, where would you hold an instance of
TinyClass ?? And when you realise that you need a private field anyway, what
is the difference in visibilty between having aTiny.NextVal as a private
field or just nextVal ??

As I said before, you could declare a private static class with one static
property, thus avoiding the need for a local variable or field, but then
that would also be visible to, at least, all members of the containing
class.

| My first mentor insisted on appropriate data scoping. If foo is the only
| thing that should see NextVal, it must be the only thing that can see
| NextVal. In C#, so far this seems to require a mini-class.

I understand the requirement to scope appropriately, but the mini-class will
not enforce any- stricter scoping. C# simply does not allow any stricter
scoping than a private field in the class that contains the method.

Joanna
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top