Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Common methods for different objects
Message
 
 
À
21/05/2009 13:04:25
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 3.0
OS:
Windows XP
Network:
Windows 2003 Server
Database:
MS SQL Server
Divers
Thread ID:
01400964
Message ID:
01402566
Vues:
52
>It's also possible to instantiate Helper each time you need it, then it won't be a member of neither test1 nor test2
>
>
>	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.

Now, how would I change this to use the Helper class instead?

Thanks again.

UPDATE. Solved.
If it's not broken, fix it until it is.


My Blog
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform