473,499 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 16367
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3917
by: Datta Patil | last post by:
Hi , #include<stdio.h> func(static int k) /* point2 : why this is not giving error */ { int i = 10 ; // static int j = &i ; /* point 1: this will give compile time error */ return k; } /*...
9
2801
by: AnandRaj | last post by:
Hi guys, I have a few doubts in C. 1. Why static declartions are not allowed inside structs? eg struct a { static int i; }; Throws an error ..
28
4573
by: Dennis | last post by:
I have a function which is called from a loop many times. In that function, I use three variables as counters and for other purposes. I can either use DIM for declaring the variables or Static. ...
12
2234
by: rodchar | last post by:
hey all, vb has a static keyword for variables, what is charp's equivalent,please? static tempVar as String thanks, rodchar
55
6149
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
3
2905
by: dmitrey | last post by:
Thank you in advance, Dmitrey
14
5975
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
10
2632
by: Pramod | last post by:
Hello to all of you, I want to know that what's the use to create static object. Thanks You Pramod Sahgal
11
8290
by: Jef Driesen | last post by:
I have the following problem in a C project (but that also needs to compile with a C++ compiler). I'm using a virtual function table, that looks like this in the header file: typedef struct...
0
7134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7392
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5479
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4920
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4605
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3105
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3101
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1429
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.