How to implement instance numbering?

Posted by Joan Venge on Stack Overflow See other posts from Stack Overflow or by Joan Venge
Published on 2011-02-18T23:06:41Z Indexed on 2011/02/18 23:25 UTC
Read the original article Hit count: 155

Filed under:
|

I don't know if the title is clear but basically I am trying to implement something like this:

public class Effect
{
    public int InternalId ...

    public void ResetName() ...
}

When ResetName is called, this will reset the name of the object to:

"Effect " + someIndex;

So if I have 5 instances of Effect, they will be renamed to:

"Effect 1"
"Effect 2"
"Effect 3"
...

So I have another method (ResetNames) in another manager/container type that calls ResetName for each instance. And right now I have to pass an integer to ResetName while keeping a counter myself inside ResetNames. But this feels not as clean and this prevents me from calling ResetName myself outside the manager class, which is valid.

How to do this better/cleaner?

As for the InternalId, it's just some id that stores the creation order for everything. So I can't just rely on these, because the numbers are large, like 32000, etc.

EDIT: Container ResetNames code:

int count = 1;
var effects = this.Effects.OrderBy ( n => n.InternalId );
foreach ( Effect effect in effects )
{
    effect.ResetName ( count );
    ++count;
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET