即使定义了函数也必须在类中实现函数的错误 [英] Error that I must implement a function in a class even though function is defined

查看:76
本文介绍了即使定义了函数也必须在类中实现函数的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到错误:类'QueryParameterComparer'必须为接口'System.Collections.Generic.IComparer(Of QueryParameter)实现'函数Compare(x作为QueryParameter,y作为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

我也尝试将其完全写出来:

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

我缺少什么?

推荐答案

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

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

这篇关于即使定义了函数也必须在类中实现函数的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆