0

I am new to c# and am having issues with the syntax and passing an object. I am building a form with a treeview of stores and a list view of customers for each store. When I click a button on the form, 'OnStoreAdd' is called and creates the store object. How do I pass that object to '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? } } } 

I don't know how to pass the new store object into the AddStoreNode method. The tag in AddStoreNode method is so that my listview can access treeview nodes. Do I need another tag in the StoreAdd method to use?

Any information that could point me in the right great direction would be greatly appreciated!

1
  • Makes no sense to me. Why would you have a class store that has a property Customers and a Dictionary( Store, Customers)? Commented Feb 18, 2015 at 20:36

1 Answer 1

1

I would change the business layer AddStore to return the store object added to the internal list

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; } } } 

Now in the user interface you could get back the same instance

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; } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.