Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
564 views
in Technique[技术] by (71.8m points)

interface - C# Setting Item Property State

I would like to set a property called "Current" which give me the opportunity to keep track of which item I'm working with. Here's my class Volume:

namespace ConsoleApp3 {
class Volume : Interface
{
    public string Name { get; set; }
    public Volume()
    {

    }
 }

My interface called: "Interface"

namespace ConsoleApp3{
class Interface
{
    public string Name { get; set; }
    public int Id { get; set; }
    public bool IsCurrent { get; set; }
}

And my RootList Class:

namespace ConsoleApp3{
class RootList<T> : List<T> where T : Interface, new()
{
    private int _index;
    private int _id;
    public RootList()
    {

    }

    public T First
    {
        get => this[0];
        set => this[0] = value;
    }

    public T Last
    {
        get => this[this.Count - 1];
        set => this[this.Count - 1] = value;
    }
  public T Current
    {
        get
        {
            return this.FirstOrDefault(tTemp => tTemp.IsCurrent == true);
        }
        set
        {
            this.RmCurrent();
            int _index = this.IndexOf(value);
            this[_index].IsCurrent = true;
        }
    }

    public T this[string name]
    {
        get
        {
            return this.FirstOrDefault(tTemp => tTemp.Name == name);
        }
    }
   private void RmCurrent()
    {
        var _iscurrent = this.Where(v => v.IsCurrent = true);
        foreach (var item in _iscurrent)
        {
            item.IsCurrent = false;
        }
    }

    public void Add()
    {
        this.Add(new T());
        this.RmCurrent();
        this.Last.IsCurrent = true;
    }
}

}

I'm using the "IsCurrent" property to track my items: my item has the "Current" state if the "IsCurrent" property is "true". Only one item can me the "Current" one at the same time. My "Current" method doesn't work. For exmaple:

        VolumeList.Add();
        VolumeList.Last.Name = "test_0";
        VolumeList.Add();
        VolumeList.Last.Name = "test_1";
        VolumeList.Add();
        VolumeList.Last.Name = "test_2";

In this case, my Current item is the Last one "test_2". If I do that:

           VolumeList.Current = VolumeList[1];

I have two Current items VolumeList[0] and VolumeList[1]. Not only VolumeList[1]. As you can see, I also have a string indexer in my RootList so it has to work with int indexer and string indexer. Do you have any ideas?

Thanks a lot.

Best regards,


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I would not search for an item which is current in private void RmCurrent()

I would only do

private void RmCurrent()
    {
        foreach (var item in this)
        {
            item.IsCurrent = false;
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...