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:
01401401
Vues:
37
No, #2 could not work for the scenario I had - 3 grids with very similar MouseUp method.


>(2)
>The idea is simple. There are two different classes, test1 and test2, that need some methods that do the same
>In both test1 and test2, I add an object (Helper) which has a method sum()
>
>Sum() can be called both from within test1 and test2
>
>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;
>		}
>	}
>
If it's not broken, fix it until it is.


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

Click here to load this message in the networking platform