我是c#的新手,我遇到了语法问题并传递了一个对象。我正在构建一个带有商店树视图的表单以及每个商店的客户列表视图。当我点击表单上的按钮时,' OnStoreAdd'被调用并创建商店对象。如何将该对象传递给' AddStoreNode(对象?标签?)'?
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方法中的标记使我的listview可以访问treeview节点。我是否需要使用StoreAdd方法中的其他标记?
任何可以指向正确方向的信息都将非常感谢!
答案 0 :(得分: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;
}