"static" variable

  • Thread starter Thread starter Luc Bolly
  • Start date Start date
L

Luc Bolly

Hi.

I would like to create a variable that allows me to count each pass through
a function. Ex.

class test {
static void main () {
DoFunction();
Environnment.Exit(0);
}

static void DoFunction() {
//static MyCounter=0; // this line should allow me to create
a variable that's create a compil time, initialized
// at compile time and
that will retain its value even when leaving the function
if ((MyCounter++%10)==0) {
Console.WriteLine(MyCounter);
}
}
}

I know that I can do the job with a variable external to the function, but I
used to work this way in older language (e.g. Clipper 5) and I founded it
useful. But I think that this construct is not allowed in C# ! Anyone having
an idea ?

Thanks
Luc
 
Currently C# doesn't support static locals. Instead you would create a
static field in your current class(which, if the language supported static
locals, is what it would do anyway).
for example
class test
{
static MyCounter=0;
static void DoFunction()
{
if ((MyCounter++%10)==0) {
Console.WriteLine(MyCounter);
}
}
}
 
Luc Bolly said:
I would like to create a variable that allows me to count each pass through
a function. Ex.

class test {
static void main () {
DoFunction();
Environnment.Exit(0);
}

static void DoFunction() {
//static MyCounter=0; // this line should allow me to create
a variable that's create a compil time, initialized
// at compile time and
that will retain its value even when leaving the function
if ((MyCounter++%10)==0) {
Console.WriteLine(MyCounter);
}
}
}

I know that I can do the job with a variable external to the function, but I
used to work this way in older language (e.g. Clipper 5) and I founded it
useful. But I think that this construct is not allowed in C# ! Anyone having
an idea ?

Yes - it's very deliberate, because all your state (which is what this
is) should be in your class, not your method. If a method has state,
either that state should be relevant to the class it's in, or the
method should be in its own class.
 
Daniel,

Static members are not thread safe, you have to make them thread safe.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Daniel Jin said:
quick question, is that thread safe? or do you have to make it thread
safe. I'm not quite 100% sure how ++ deal with threading. also, can you
mark the counter volatile and not use lock if it's not thread safe?
----- Daniel O'Connell [C# MVP] wrote: -----

class test
{
static MyCounter=0;
static void DoFunction()
{
if ((MyCounter++%10)==0) {
Console.WriteLine(MyCounter);
}
}
}
 
Daniel Jin said:
quick question, is that thread safe? or do you have to make it thread
safe. I'm not quite 100% sure how ++ deal with threading. also, can
you mark the counter volatile and not use lock if it's not thread
safe?

No, it's not thread-safe. MyCounter++ acts as:

MyCounter = MyCounter+1;

Another thread could change the value of MyCounter between you reading
it and you then writing the new value.

Marking if as volatile doesn't help - it doesn't change any of the
above.

However, you can use Interlocked.Increment (ref MyCounter) to increment
in a thread-safe way.

Personally I'd usually just use a lock, as with any other access to
data which may be shared between threads.
 
Back
Top