The response I received to a recent post got me investigating collections, they're pretty cool. I am attempting to add a CompareTo function to a class that I've written so that I can use the Sort method. I have used Listing 4 in a DevX article as my pattern for the CompareTo function.
Public Class Recipe
Implements IComparable
'...a property, RecipieID, is created & stored internally as intID
Public Overloads Function CompareTo(ByVal objRecipe As Recipe) _
As Integer Implements Icomparable.CompareTo
Return intID.CompareTo(objRecipe.RecipeID)
End Function
End Class
In my version of the CompareTo function I must declare objRecipe As Object, if I declare objRecipe As Recipe, I get a compile error. The error is: 'CompareTo' cannot implement 'CompareTo' because there is no matching function on interface 'System.IComparable'.
I do not understand the error message & why my syntax is wrong. You help in my continuing education would be greatly appreciated. Thanks.

Working with IComparable
huahsin68
Bhanu,
While my code is working and has always worked, my original question regarding the error message and and syntax is still a mystery to me. Perhaps, if I leave the question marked as unanswered, someone skilled in the art of VB Express will be able to answer these questions. Thanks.
Muhammet TUR?AK
If you have 2005 then implement IComparer(Of Recipe) so that you dont have to cast to recipe.
Then fill in the compare function it lays out. Lets say your recipe class has a property NumberOfEggs and you want to sort by how eggy the recipe is.
Public Function Compare(ByVal x As Recipe, ByVal y As Recipe) As Integer Implements System.Collections.Generic.IComparer(Of Recipe).Compare
If x.numberOfEggs > y.numberOfEggs Then Return 1
If x.numberOfEggs < y.numberOfEggs Then Return -1
Return 0
End Function
If you have 2003, then you can't use (Of Recipe), and so x and y will be Objects. Just cast them to Recipes:
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
Dim recipeX As Recipe = DirectCast(x, Recipe)
Dim recipeY As Recipe = DirectCast(y, Recipe)
if blah blah... return blah blah
End Function
edit: oh, its the express forum, doh... IComparer(Of Recipe) is fine then...
greenmind
Hi,
FYI: If your post is answered then could you please mark it as answered.
Thank you,
Bhanu.
cfendya
jo0ls,
Thanks for all your effort replying. I have used IComparer in VB 2003. IComparable looked pretty cool, so I thought that I'd give it a go. I could add the CompareTo function to the Class and then sort a List of those objects.
The code appears to work; my main concern was understanding the syntax, the error message and improving my knowledge of VB Express. Thanks again.