传递对象 C# 和 .NET
Passing an object C# and .NET
我是 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 方法中使用另一个标签吗?
任何能为我指明正确方向的信息都将不胜感激!
我会将业务层AddStore
更改为return将商店对象添加到内部列表
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;
}
我是 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 方法中使用另一个标签吗?
任何能为我指明正确方向的信息都将不胜感激!
我会将业务层AddStore
更改为return将商店对象添加到内部列表
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;
}