471,319 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

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

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
Jan 4 '06 #1
5 16088
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
:)

--
Colin Neller
http://www.colinneller.com/blog
"John Kelsey" <ke*****@tacits.com> wrote in message
news:ez**************@TK2MSFTNGP11.phx.gbl...
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

Jan 4 '06 #2
"John Kelsey" <ke*****@tacits.com> a écrit dans le message de news:
ez**************@TK2MSFTNGP11.phx.gbl...

| 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/arch...11/130248.aspx

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

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jan 4 '06 #3
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

"John Kelsey" wrote:
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

Jan 4 '06 #4

"Joanna Carter [TeamB]" <jo****@not.for.spam> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
"John Kelsey" <ke*****@tacits.com> a écrit dans le message de news:
ez**************@TK2MSFTNGP11.phx.gbl...
--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
Jan 4 '06 #5
"John Kelsey" <ke*****@tacits.com> a écrit dans le message de news:
u%****************@TK2MSFTNGP10.phx.gbl...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jan 4 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Datta Patil | last post: by
9 posts views Thread by AnandRaj | last post: by
12 posts views Thread by rodchar | last post: by
14 posts views Thread by Jess | last post: by
10 posts views Thread by Pramod | last post: by
11 posts views Thread by Jef Driesen | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.