mshvw

I try to understand an IComparable code example.

MyIndividual object uses the IComparable interface with the
CompareTo memberfunction.

MyGroup object contains a private ArrayList with several
MyIndividual objects.

When executing the MyIndividual.Sort function the ComparTo
memberfunction is called by the operating system many
times to do the comparison.

So far so good.

The CompareTo memberfunction compares some property of the
received argument object against the same property of the
'this' object.

Now I don't understand how the 'this' reference is pointing
to one of the MyIndividual objects.

Which or What is responsible for initializing the 'this'
reference

I am confused as normally I think when memberfunction code is
executed, that object is somehow accessed from my program,
so my program selected one of the MyIndividual objects,
but if the operating system is calling one of my
memberfunctions (CompareTo) which or what process selects
one of the MyIndividual objects

I hope you will understand my question.

Regards,

Henk



Re: Visual C# Language Trying to understand IComparable

boban.s

'this.' referes to current instance of a class, but you can remove it if you don't like it. CompareTo method implemented as a requirement of IComparable interface can't be static so it must be called on previously created instance of the class.




Re: Visual C# Language Trying to understand IComparable

mshvw

>'this.' referes to current instance of a class,

Yes, I agree, but that touches the essence of my question which I will narrow by putting it in other words::

Which or What process selects one of the instances to be the 'current' instance and How and Where is that specific instance selection exactly handled

Henk





Re: Visual C# Language Trying to understand IComparable

mshvw

Well, I gave it a little thought and I think this is the
way how it goes:

MyGroup executes a MyIndividual(ArrayList).Sort and
therefore the operating system (OS) knows that sorting
objects are of kind MyIndividual.
Now the OS accesses the individual MyIndividual(ArrayList)
objects at it's own (OS) algorithm.
So the OS selects an MyIndividual object and executes it's
associated CompareTo function. Therefore, when a CompareTo
function is executed, the 'this' reference points to the
MyIndividualobject that was selected by the OS before the
OS called that specific CompareTo function.

;-) ;-) ;-)

Regards,

Henk





Re: Visual C# Language Trying to understand IComparable

Christian Liensberger - MSP

It's actually .NET that sorts the list and it's directly implemented in the list. .NET uses for this cases Quicksort, because that is the fastest one. If you had a look at quicksort you will see that items need to be compared and the list is splitted (divide and conquer tactic).

IComparable is therefore needed to compare two items to understand which one is to place where...

IComparable and sort is very powerful, because you can have one property that is checked and if for both objects that property has the same value you can check a second one. That makes it very powerful and useful.