D
DancnDude
I have a class that needs to have several different kinds of sorting
routines on an ArrayList that it needs to conditionally do based upon the
data. I have successfully created a class that implements IComparer and
does the sort correctly for one of these sort types. This class's
definition is like:
// Sort class that implements IComparer so
public class CompareFields1 : IComparer
{
public int Compare(object a, object b)
{
// Code that appears in every sort's Compare routine
if (a == b) return 0;
if (a == null) return -1;
if (b == null) return 1;
if (a.GetType() != b.GetType())
{
throw new ArgumentException();
}
// Now that the inputs are valid, do the actual compare
/*... ...actual compare code here... ...*/
}
}
Now I want to make another sort algorithm (CompareFields2). This definition
will be have the same layout as the above one, but the actual compare code
is going to be different. I would like to try and factor out the common
code, but I can't seem to think of a good class design for it since this
isn't in the constructor. I thought of factoring the code into a utility
class, but that requires me to call the utility class and get the response
back, which is still about 5 lines of code I would need to duplicate. Any
ideas on what I should be doing?
Thanks!
routines on an ArrayList that it needs to conditionally do based upon the
data. I have successfully created a class that implements IComparer and
does the sort correctly for one of these sort types. This class's
definition is like:
// Sort class that implements IComparer so
public class CompareFields1 : IComparer
{
public int Compare(object a, object b)
{
// Code that appears in every sort's Compare routine
if (a == b) return 0;
if (a == null) return -1;
if (b == null) return 1;
if (a.GetType() != b.GetType())
{
throw new ArgumentException();
}
// Now that the inputs are valid, do the actual compare
/*... ...actual compare code here... ...*/
}
}
Now I want to make another sort algorithm (CompareFields2). This definition
will be have the same layout as the above one, but the actual compare code
is going to be different. I would like to try and factor out the common
code, but I can't seem to think of a good class design for it since this
isn't in the constructor. I thought of factoring the code into a utility
class, but that requires me to call the utility class and get the response
back, which is still about 5 lines of code I would need to duplicate. Any
ideas on what I should be doing?
Thanks!