开发者

How to implement instance numbering?

I don't know if the title is clear but basically I am try开发者_开发技巧ing 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;
}


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


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


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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜