-6

我有以下课程:

  public class Core {
       SyncList<myItem> ITEM = new SyncList<myItem>;

       public void addSubitem(){
           subItem i = new subItem();
           i.itemType = "TYPE1"; // not updating
           ITEM[0].sItem.Add(i);
       }
  }

  public class myItem {
       public SyncList<subItem> = sItem new SyncList<subItem>();
  }

  public class subItem {

       public string itemType { get; set; }

       public subItem(){
           this.itemType = "TYPE1"; // not updating
       }
  }

这就是我在主窗体上定义它的方式:

public static Core core { get; set; }
core = new Core(); // assigned in form constructor

这就是我称之为 onClick 事件的方式:

core.addSubitem();

但它不会更新,并且 itemType 变量一直为空。我不明白为什么会这样..有什么想法吗?谢谢!

4

2 回答 2

1

如果我理解正确,您想将 SubItem 添加到您的项目列表中。你不能像你尝试做的那样直接做。

这是我的建议:

 public class Core {
       public SyncList<MyItem> Items{get; private set;}

       public Core(){
            Items = new SyncList<MyItem>;
       }

       public void AddSubItem(){
            MyItem item = new MyItem();
            SubItem i = new SubItem();
            i.ItemType = "TYPE1";
            item.SubItems.Add(i);
            Items.Add(item);           
       }
  }

  public class MyItem {
       public SyncList<SubItem> SubItems {get; private set;}

       public SubItem(){
            SubItems = new SyncList<SubItem>();
       }
  }

  public class SubItem {

       public string ItemType { get; set; }
  }

然后在你的主要形式:

public static Core Core { get; set; }
Core = new Core(); // assigned in form constructor

在您的点击事件中,像这样调用您的方法:

Core.AddSubItem();
于 2013-05-24T11:58:09.453 回答
1

我不知道 SyncList 是什么,但我用 List<>() 尝试了你的代码。有一些问题,现在可以工作了。

namespace Test
{
    public class Core
    {
        List<MyItem> MyItemList = new List<MyItem>();

        public void AddSubitem()
        {
            SubItem sItem = new SubItem();
            sItem.ItemType = "TYPE2"; // it's updating

            MyItem mItem = new MyItem();
            this.MyItemList.Add(mItem);

            this.MyItemList[0].sItem.Add(sItem);
        }
    }

    public class MyItem
    {
        public List<SubItem> sItem = new List<SubItem>();
    }

    public class SubItem
    {
        public string ItemType { get; set; }

        public SubItem()
        {
            this.ItemType = "TYPE1"; // it's updating
        }
    }
}

在下面的代码 ItemType 的值为 TYPE2 之后。

Core core = new Core();
core.AddSubitem();
于 2013-05-24T11:58:30.057 回答