7

I get the error: Class 'QueryParameterComparer' must implement 'Function Compare(x As QueryParameter, y As QueryParameter) As Integer' for interface 'System.Collections.Generic.IComparer(Of QueryParameter)'.

On this class definition:

    Protected Class QueryParameterComparer
        Implements IComparer(Of QueryParameter)

        Public Function Compare(x As QueryParameter, y As QueryParameter) As Integer
            If x.Name = y.Name Then
                Return String.Compare(x.Value, y.Value)
            Else
                Return String.Compare(x.Name, y.Name)
            End If
        End Function

    End Class

I also tried writing it out fully:

    Protected Class QueryParameterComparer
        Implements System.Collections.Generic.IComparer(Of QueryParameter)

        Public Function Compare(x As QueryParameter, y As QueryParameter) As Integer
            If x.Name = y.Name Then
                Return String.Compare(x.Value, y.Value)
            Else
                Return String.Compare(x.Name, y.Name)
            End If
        End Function

    End Class

What am I missing?

Adam
  • 6,041
  • 36
  • 120
  • 208
  • 4
    Interface method implementation requires the *Implements* keyword. Just let the IDE help you fall in the pit of success. Delete the Function, put the cursor after the Implements yadayada line and press the Enter key. – Hans Passant May 19 '15 at 14:54
  • 1
    Woah! I've never seen that happen before! I just marked this as a duplicate and then I realized that you were the one that asked the duplicate question years ago. Funny... – Steven Doggart May 19 '15 at 14:59
  • @StevenDoggart: LOL! :S Some people never learn? :) Can't delete this post anymore though.... – Adam May 19 '15 at 15:15

2 Answers2

11

Unlike in c#, where the name of the method just has to match the one in the interface, in VB.NET, all interface implementations must always be explicitly stated with Implements keywords on each member:

Protected Class QueryParameterComparer
    Implements IComparer(Of QueryParameter)

    Public Function Compare(x As QueryParameter, y As QueryParameter) As Integer Implements IComparer(Of QueryParameter).Compare
        ' ...
    End Function
End Class
Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
3

VB.Net requires you to specify which methods are the implementation methods of your interfaces.

Public Function Compare(x As QueryParameter, y As QueryParameter) As Integer Implements System.Collections.Generic.IComparer(Of QueryParameter).Compare

It's odd, but it does allow for you to specify a different function name for the implementation. This makes it so that a direct access to your class can have one name for the function, but a reference through the interface would have the interface method name. Something else you can do is specify the method a Private so that you can only access the method through an interface reference.

Scott
  • 370
  • 2
  • 7