我必须在 class 中实现函数的错误,即使函数已定义
Error that I must implement a function in a class even though function is defined
我收到错误:Class 'QueryParameterComparer' must implement 'Function Compare(x As QueryParameter, y As QueryParameter) As Integer' for interface 'System.Collections.Generic.IComparer(Of QueryParameter)'.
关于这个 class 定义:
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# 中,方法的名称只需要与接口中的名称相匹配,在 VB.NET 中,所有接口实现必须始终在每个成员上用 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要求您指定哪些方法是您接口的实现方法。
Public Function Compare(x As QueryParameter, y As QueryParameter) As Integer Implements System.Collections.Generic.IComparer(Of QueryParameter).Compare
这很奇怪,但它确实允许您为实现指定不同的函数名称。这使得直接访问您的 class 可以有一个函数名称,但通过接口的引用将有接口方法名称。您可以做的另一件事是将方法指定为 Private,这样您就只能通过接口引用访问该方法。
我收到错误:Class 'QueryParameterComparer' must implement 'Function Compare(x As QueryParameter, y As QueryParameter) As Integer' for interface 'System.Collections.Generic.IComparer(Of QueryParameter)'.
关于这个 class 定义:
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# 中,方法的名称只需要与接口中的名称相匹配,在 VB.NET 中,所有接口实现必须始终在每个成员上用 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要求您指定哪些方法是您接口的实现方法。
Public Function Compare(x As QueryParameter, y As QueryParameter) As Integer Implements System.Collections.Generic.IComparer(Of QueryParameter).Compare
这很奇怪,但它确实允许您为实现指定不同的函数名称。这使得直接访问您的 class 可以有一个函数名称,但通过接口的引用将有接口方法名称。您可以做的另一件事是将方法指定为 Private,这样您就只能通过接口引用访问该方法。