473,403 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 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 16359
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
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
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
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
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
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
by: dmitrey | last post by:
Thank you in advance, Dmitrey
14
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
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...

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.