Hi Gary,
I had a good old study and was horrified!!
Ok, I'm only teasing, I needn't be so alarmist. There were some things
that I didn't like so I changed them.
. But that then left a cold wind
blowing through a different hole! Swings and roundabouts coming up.
Firstly I agree with you that "sort yourselves out" has a better feel to
it than "line up over there while Mr Sorter deals with you". I think it's
better for a user if they don't have to worry about having to specify a
comparer.
Saying alFoo.Sort() is nice and simple. The list of Foos gets sorted
without user interaction. As it should be - who knows more about a Foo than
clsFoo?
Another alternative is that clsFoo defines the comparer and the user
specifies it
alFoo.Sort (clsFoo.BuiltInFooComparer1)
The last, and most common case, is that the user has to implement the
whole thing
alFoo.Sort (UserDefinedFooComparer).
Which is not hard but it's always a pause in the flow. And an odd scrap of
code that has to be stuck somewhere.
The first and second alternatives get a bit not-much-difference-there-ish
when using the non-default comparer. Because while in the second you just use
clsFoo.BuiltInFooComparer2 instead of 1, in the first you need an extra line
to tell the Foos which comparer to use. clsFoo.SortBy (ComparerId).
I had a look at what Help had to say about IComparable. There aren't many
Types that implement it - just the basic ones - Numbers, Enums, Strings (and
Versions).
Here's the bit that had me worried: In the central class, Person, each
Person has their own instance of SortPolicy (and SortPolicyChanged). That's
not good for three reasons. The first is that it's a lot of overhead for what
is probably a minor part of a Person's function. Secondly every change of
SortPolicy requires a change to every Person in the array to be sorted.
Finally it's possible to assign different policies to different Persons.
Sorting an array of such would be highly unpredictable.
In finding out what implements IComparable, I had to consider - Strings
and Numbers don't carry around a SortPolicy. Programmers would be up in arms!
So it must be that the <class> impements the interface. So that was the first
change I made to the code. I specified the SortPolicy field, Property and
Changed event as Shared.
This made it easier in the DatingService, too. The loop went straight out,
to be replaced by ArrayList.Clone() and Person.SortPolicy=.... [ps. Why did
you use an enumerator rather than For Each - Is that SmallTalk talking?].
Having the class determine the SortPolicy fixes the efficiency and
security issues but that cold wind blows. What if I have two arrays of Persons
and want them sorted in different ways?
Over to you Gary. Your turn in this discussion.
Regards,
Fergus