Vb.net 错误:即使已定义函数,我也必须在类中实现函数
我得到错误:Vb.net 错误:即使已定义函数,我也必须在类中实现函数,vb.net,class,interface,interface-implementation,Vb.net,Class,Interface,Interface Implementation,我得到错误:类'QueryParameterComparer'必须实现'x作为QueryParameter,y作为QueryParameter)作为整数'for interface'System.Collections.Generic.IComparer(属于QueryParameter)'。 关于此类定义: Protected Class QueryParameterComparer Implements IComparer(Of QueryParameter)
类'QueryParameterComparer'必须实现'x作为QueryParameter,y作为QueryParameter)作为整数'for interface'System.Collections.Generic.IComparer(属于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#不同,c#中的方法名称必须与接口中的方法名称匹配,在VB.NET中,所有接口实现必须始终在每个成员上明确声明实现关键字:
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
Net要求您指定哪些方法是接口的实现方法
Public Function Compare(x As QueryParameter, y As QueryParameter) As Integer Implements System.Collections.Generic.IComparer(Of QueryParameter).Compare
这很奇怪,但它确实允许您为实现指定不同的函数名。这使得对类的直接访问可以有一个函数名,但通过接口的引用将有接口方法名。您还可以将该方法指定为私有方法,以便只能通过接口引用访问该方法。接口方法实现需要Implements关键字。让IDE帮助您落入成功的陷阱。删除该功能,将光标放在yadayada行之后,然后按Enter键。哇!我以前从未见过这种情况!我只是把这个标记为一个副本,然后我意识到你是多年前问这个重复问题的人。好笑…@Stevendogart:LOL!:有些人永远学不会但无法再删除此帖子。。。。