【问题标题】:Passing an object C# and .NET传递对象 C# 和 .NET
【发布时间】:2015-02-18 20:23:47
【问题描述】:

我是 C# 新手,在语法和传递对象方面遇到问题。我正在构建一个表格,其中包含商店的树形视图和每个商店的客户列表视图。当我单击表单上的按钮时,会调用“OnStoreAdd”并创建存储对象。如何将该对象传递给“AddStoreNode(object?tag?)”?

namespace CustomerInfoObjects
{
    public class Store
    {
        private List<Customer> _customers = new List<Customer>();

        public List<Customer> Customers
        {
            get { return _customers; }
            set { _customers = value; }
        }

        private string _name = string.Empty;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
    }
}

namespace DomainObjects
{
    public class CustomerList : List<Customer> { }
    public class StoreToCustomerListDictionary : Dictionary<Store, CustomerList> { }
    public class CustomerInfoDocument
    {
        private StoreToCustomerListDictionary _storeToCustomerList = new StoreToCustomerListDictionary();
        public StoreToCustomerListDictionary StoreToCustomerList
        {
            get { return _storeToCustomerList; }
            set { _storeToCustomerList = value; }
        }
    }
}

namespace BusinessLayer
{
    public static class CustomerMgr
    {
        private static CustomerInfoDocument _document = new CustomerInfoDocument();

        public static bool AddStore(string storeName)
        {
            Store store = new Store();
            store.Name = storeName;
            _document.StoreToCustomerList.Add(store, null);
            return true;
        }
    }
}

namespace UILayer
{
    public partial class StoreTreeControl : UserControl
    {
        public StoreTreeControl()
        {
            InitializeComponent();
        }

        public void AddStoreNode(Store object? )    //What type of argument?
        {
            TreeNode node = _tvwStores.Nodes.Add(store.Name);
            node.Tag = store;
            _tvwStores.SelectedNode = node;
        }

        private void OnStoreAdd(object sender, System.EventArgs e)
        {
            StorePropertiesForm f = new StorePropertiesForm();
            if (f.ShowDialog() != DialogResult.OK)  
                return;                             

            if (!CustomerMgr.AddStore(f.StoreName))
            {
                MessageBox.Show("Store name should no be blank");
                return;
            }

            AddStoreNode(?);  //What argument would I use here?
        }
    }
}

我不知道如何将新的商店对象传递给 AddStoreNode 方法。 AddStoreNode 方法中的标记使我的列表视图可以访问树视图节点。我需要在 StoreAdd 方法中使用另一个标签吗?

任何可以为我指明正确方向的信息将不胜感激!

【问题讨论】:

  • 对我来说毫无意义。为什么你会有一个类商店,它有一个属性客户和一个字典(商店,客户)?

标签: c# .net object treeview parameter-passing


【解决方案1】:

我会更改业务层AddStore 以返回添加到内部列表的商店对象

namespace BusinessLayer
{
    public static class CustomerMgr
    {
        private static CustomerInfoDocument _document = new CustomerInfoDocument();

        public static Store AddStore(string storeName)
        {
            if(string.IsNullOrWhiteSpace(storeName))
                return null;

            Store store = new Store();
            store.Name = storeName;
            _document.StoreToCustomerList.Add(store, null);
            return store;
        }
    }
}

现在在用户界面中您可以返回相同的实例

private void OnStoreAdd(object sender, System.EventArgs e)
{
    StorePropertiesForm f = new StorePropertiesForm();
    if (f.ShowDialog() != DialogResult.OK)  
        return;                             

    Store currentStore = CustomerMgr.AddStore(f.StoreName);
    if (currentStore == null)
    {
        MessageBox.Show("Store name should no be blank");
        return;
    }

    AddStoreNode(currentStore);  
}
public void AddStoreNode(Store aStoreToAdd)    
{
    TreeNode node = _tvwStores.Nodes.Add(aStoreToAdd.Name);
    node.Tag = aStoreToAdd;
    _tvwStores.SelectedNode = node;
}

【讨论】:

    猜你喜欢
    • 2018-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多