Joan Venge
Joan Venge

Reputation: 330912

How to implement instance numbering?

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;
}

Upvotes: 0

Views: 145

Answers (5)

BlueMonkMN
BlueMonkMN

Reputation: 25601

Instead of having Name be a stored property, could you do something like this?

public class Effect
{
    public int InternalId ...
    public int Index;
    public string BaseName;
    public string Name
    {
       get
       {
          return BaseName + index;
       }
    }
}

Upvotes: 0

PeterL
PeterL

Reputation: 1397

Have a manager class that handles the naming. It will also handle creation of the child class, and will embed a reference to itself. You can now call ResetName() on the child class, and it will have it's manager handle whatever logic needs to be done.

I'm not sure exactly what you want the results to be in various situations, but hopefully the following will be of some help:

public class Effect {
{
  private EffectManager _manager;
  public string Name {get;set;}

  public Effect(EffectManager manager) {
    _manager = manager;
  }

  public void ResetName() {
    Name = _manager.GetNextName();
  }
}

public class EffectManager {
  private List<Effect> Effects;
  private int currentIndex;
  public Effect CreateEffect() {
    var e = new Effect(this);
    Effects.Add(e);
  }

  public string GetNextName() {
    return "Effect " + currentIndex++;
  }

  public void ResetAllNames() {
    currentIndex = 0;
    foreach(var effect in Effects) {
      effect.Name = GetNextName();
    }
  }
}

Upvotes: 2

AMissico
AMissico

Reputation: 21684

Namespace Diagnostics

    <Conditional("DEBUG")> _
    Public NotInheritable Class UniqueID

        Private Shared _idBase As Integer

        Private Sub New()
            'keep compiler from creating default constructor
        End Sub

        Public Shared Function GetNext() As String
            Return "ID" + System.Threading.Interlocked.Increment(_idBase).ToString("00")
        End Function

    End Class

End Namespace

Upvotes: 0

dkackman
dkackman

Reputation: 15549

Are the names specific to all instance or to all instances in a given collection?

If the former you could do something like:

public class Effect
{
    private static int _lastId;

    public Effect()
    {
        InternalId = _lastId++;
    }
    public string Name 
    {
        get { return "Effect" + InternalId.ToString(); }
    }
    public int InternalId ...
}

Upvotes: 0

KBoek
KBoek

Reputation: 5976

One of many possibilities: give your Effect class a public property Name, and in the method where you populate a list or array of Effect objects, assign the name. You can also give the Effect class an integer property and set the number, so that you can sort them, if you want.

public class Effect() 
{  
  public string Name() { get; set; }
}

public class SomeClass()
{
  private List<Effect> Effects;

  public static void WhateverMethod()
  {
    for (var i = 0; i < Effects.Count; i++)
      Effects[i].Name = "Effect " + (i + 1).ToString();
  }
}

Upvotes: 0

Related Questions