> public class test1 > { > SumHelper Helper = new SumHelper(); > > public void test() > { > List<int> theList = new List<int> { 1, 2, 3, 4 }; > > int result = Helper.Sum(theList); > } > } > > public class test2 > { > SumHelper Helper = new SumHelper(); > > public void test() > { > List<int> theList = new List<int> { 7,8,9,10 }; > > int result = Helper.Sum(theList); > } > } > > public class SumHelper > { > public int Sum(List<int> theList) > { > int total = 0; > foreach (int item in theList) > { > total += item; > } > return total; > } > } >>An alternative (suggested by Tim) is to have a helper class with a static method
> public class test1 > { > > public void test() > { > List<int> theList = new List<int> { 1, 2, 3, 4 }; > > int result = SumHelper.Sum(theList); > } > } > > public class test2 > { > > public void test() > { > List<int> theList = new List<int> { 7,8,9,10 }; > > int result = SumHelper.Sum(theList); > } > } > > public class SumHelper // the class should be static if all of its methods are static > { > public static int Sum(List<int> theList) > { > int total = 0; > foreach (int item in theList) > { > total += item; > } > return total; > } > } >I have another scenario where I think I need some common Helper class - but not 100% sure how to implement. I have two forms where I have Customer Combobox. I need to Load the entries to it. This is the method that does it currently (coded in one of the forms)
private void LoadCustomersCombo() { //set up the customer datasource for the drop down. Dictionary<Guid, string> _customersEntries = this._CustomerListBiz.GetCustomersDictionary(); this.cboCustomer.Items.Clear(); this.cboCustomer.DisplayMember = "Value"; this.cboCustomer.ValueMember = "Key"; foreach (KeyValuePair<Guid, string> kvp in _customersEntries) { this.cboCustomer.Items.Add(kvp); } }where _customerEntries is a private property of the form.