Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error that I must implement a function in a class even though function is defined [duplicate]

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?

like image 731
Adam Avatar asked May 19 '15 14:05

Adam


Video Answer


1 Answers

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
like image 129
Steven Doggart Avatar answered Oct 05 '22 23:10

Steven Doggart