Search code examples
vb.neticomparable

Compiler doesn't see CompareTo method in IComparable(Of T) object


I'm trying to apply the answer to Implementing generic IComparer in VB to my project by implementing an IComparable interface for a class in VB.NET. The section for the GenericComparer in that answer compiles fine, but the IComparable interface on my specific object won't get past the compiler.

Public Class RowAndRanking
    Implements IComparable(Of RowAndRanking)

    Public html As String
    Public rank As Double

    Public Function CompareTo(other As RowAndRanking) As Integer
        Return Math.Round(Me.rank - other.rank)
    End Function
End Class

The compiler keeps insisting that "Class 'RowAndRanking' must implement 'Function CompareTo(other As RowAndRanking) As Integer' for interface 'System.IComparable(Of RowAndRanking)'.", but looking at my code, I can see that method signature. Furthermore, if I go to where I'm trying to run a Sort on a List of these objects, I can type:

Dim row as RowAndRanking = new RowAndRanking
row.CompareTo(...

And Visual Studio's code complete picks up the method signature.

I've tried cleaning and rebuilding the project, but the issue remains. I've tried changing it to use a non-generic comparer solution, but the compiler still doesn't see the CompareTo method. This should be simple, but the compiler just doesn't see the function. Has this happened to anyone else? Is there something else that I can try?


Solution

  • Unlike C#, VB requires that you explicitly mark implementing methods.

    Add

    Implements IComparable(Of RowAndRanking).CompareTo