This question already has answers here:
Class 'QueryParameterComparer' must implement Function Compare.

(2 个回答)


已关闭6年。




我收到错误:Class 'QueryParameterComparer' must implement 'Function Compare(x As QueryParameter, y As QueryParameter) As Integer' for interface 'System.Collections.Generic.IComparer(Of QueryParameter)'.
关于这个类的定义:
    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

我也试着把它完整地写出来:
    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

我错过了什么?

最佳答案

与在 c# 中方法的名称只需匹配接口(interface)中的名称不同,在 VB.NET 中,所有接口(interface)实现必须始终在每个成员上使用 Implements 关键字显式声明:

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

关于vb.net - 即使定义了函数,我也必须在类中实现函数的错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30328991/

10-11 12:23