Inheritance sort question

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!
 
N

Nicholas Paldino [.NET/C# MVP]

DancnDude,

I would take the CompareFields1 class and rename it CompareFieldsBase.
In the Compare method, I would have the common code, and then I would have
the specific compare code call out to a virtual method. Then, have classes
that derive from CompareFieldsBase which would override the virtual method.

Hope this helps.
 
D

DancnDude

Thanks! That is exactly what I want it to do, but just couldn't think of
the right way to do it. :)

Nicholas Paldino said:
DancnDude,

I would take the CompareFields1 class and rename it CompareFieldsBase.
In the Compare method, I would have the common code, and then I would have
the specific compare code call out to a virtual method. Then, have classes
that derive from CompareFieldsBase which would override the virtual method.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

DancnDude said:
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!
 
D

DancnDude

No, it's not derived from CompareFields1. They would all be derived from
CompareBase as Nicholas suggested. The part that I couldn't think to do was
having the base's Compare method call a virtual method whose implementation
will only really exist in the derived classes. The virtual method kinda
threw me since I never had a need to use one until now.

Thanks for the suggestion!
 
D

DancnDude

That's exactly what I did and it's working wonderfully. :)

Nicholas Paldino said:
DancnDude,

Better yet, for a cleaner implementation, make the CompareFieldsBase
class abstract and make the virtual method abstract as well. This way, your
base class could not be instantiated, and you would force the override of
the method in derived classes (if you wanted this behavior, that is).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

DancnDude said:
No, it's not derived from CompareFields1. They would all be derived from
CompareBase as Nicholas suggested. The part that I couldn't think to do was
having the base's Compare method call a virtual method whose implementation
will only really exist in the derived classes. The virtual method kinda
threw me since I never had a need to use one until now.

Thanks for the suggestion!

to
hold upon
the compare
code duplicate.
Any
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top