views:

385

answers:

7

Hi,

I often have methods which are called regularly and have some "state" which has to be preserved between calls, as in:

float lastTime = 0.0f;
void Draw( float currentTime )
{
    if( currentTime - lastTime > 0.5f )
    {
        // not enough FPS
    }
    lastTime = currentTime;
}

And it drives me nuts that the global "state" field (here "lastTime") is visible throughout the whole class while it is only needed in this one method. Is there any way to limit this global field's visibility scope to only the method or to make it local, but keep it's value between calls? The same question applies to fields used by Properties (which have some logic and can't be automatic).

Thanks in advance for any suggestion...

A: 

Nope, sorry. C# doesn't allow function-level statics like C does. I'd recommend giving it an "icky" name, like Draw_Data_lastTime or something. That at least conveys information about where it should be used.

Promit
+1  A: 

In C#, no. This is what static variables are in VB.NET (and I believe C, though I'm no expert). I am fairly certain, though, that static variables in VB.NET are just compiled to IL with autogenerated names as instance fields.

Just stick to instance fields that are appropriately named (like lastDrawTime for your example).

Adam Robinson
A: 

No, the most restrictive scope is "private" which allows access from same type.

TcKs
+5  A: 

The only way to make it private in just a portion of the class would be to make it a separate object that managed this property, internal in the class.

However, I would recommend rethinking this a bit. The idea of privates it to keep a member private to the class itself - if you're worried about your class seeing its own data, it's time to refactor into smaller classes, each with their own function.

This has a "smell" of a class getting too large.

Reed Copsey
Completely agreed; just answered the same way concurrently! :-)
McWafflestix
A: 

Gosh, you're answering fast! :) Thanks, even if negatives so far..

A: 

I'm afraid what your asking is not possible.

I think your only other option is to pass the current state as a parameter in the method but that means that the calling object would be responsible for managing the state.

I also think that you could refactor your code because if the rest of your class don't need to know about this variable maybe this part could be somewhere else.

bruno conde
A: 

No, there is no way; but your question raises some interesting issues. If you are having concerns about your member variable for tracking this is visible throughout the whole class, that raises flags that perhaps the scope of the class has grown too large, and that the class needs to be refactored into several smaller classes.

In general, if you start worrying about segregation of members within your classes, you might have a situation that requires refactoring into smaller classes. Then again, you might not (situations vary), but at the very least, it's probably worth considering.

McWafflestix