The error is that I have to implement the function in the class, even if the function is defined

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

In 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 to fully state it:

  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?

+5
source share
2 answers

Unlike C #, where the method name just needs to match the name in the interface, in VB.NET, all implementations of the interface should always be explicitly specified with Implements keywords for 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 
+8
source

VB.Net requires you to indicate which methods are methods for implementing your interfaces.

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

This is strange, but it allows you to specify a different function name to implement. This makes it so that direct access to your class can have one name for the function, but the link through the interface will have the name of the interface method. Something else you can do is specify the Private method so that you can access this method only through a link to the interface.

+3
source

All Articles