vb.net 如何在VB.Net中实现两个方法同名参数不同的接口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2372726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:44:49  来源:igfitidea点击:

How to implement an interface in VB.Net when two methods have the same name but different parameters

vb.netinterfaceimplementation

提问by Arsalan Ahmed

I am a C# programmer but I have to work with some VB.Net code and I came across a situation where I have two methods on an interface with the same name but different method parameters. When I attempt to implement this interface in a class, VB.Net requires explicitly declaring "Implements MethodName" after the method signature. Since both method names are identical, this is confusing the compiler. Is there a way to get around this sort of problem? I suspect this must be a common occurrence. Any thoughts?

我是一名 C# 程序员,但我必须使用一些 VB.Net 代码,我遇到了这样一种情况,我在一个接口上有两个具有相同名称但方法参数不同的方法。当我尝试在类中实现此接口时,VB.Net 需要在方法签名后显式声明“Implements MethodName”。由于两个方法名称相同,这会使编译器感到困惑。有没有办法解决这类问题?我怀疑这一定是普遍现象。有什么想法吗?

N.B. This was more a case of the programmer not verifying that the interface in question had not changed from underneath him.

注意这更像是程序员没有验证所讨论的接口没有从他下面改变的情况。

回答by M.A. Hanin

How is this confusing the compiler? The compiler expects to find an implementation for every method signature, and distinguishes the implementations by their signatures.

这如何混淆编译器?编译器希望为每个方法签名找到一个实现,并通过它们的签名区分这些实现。

If the signatures are identical/undistinguishable (in most cases it means that the arguments are of the same types in the same order) you'll get a design-time error related to the interface, saying that the two methods cannot overload eachother as they have the same signature.

如果签名相同/无法区分(在大多数情况下,这意味着参数的类型相同,顺序相同),您将收到与接口相关的设计时错误,说明这两个方法不能重载彼此,因为它们有相同的签名。

So, in any case, the compiler should not be confused. Should you need further assistance, please attach a code sample - these things are relatively easy to resolve.

所以,无论如何,编译器不应该被混淆。如果您需要进一步的帮助,请附上代码示例 - 这些事情相对容易解决。

Tip: When writing the implementation, as soon as you write down "implements MyInterface" and hit Enter - Visual Studio will create a "skeleton" code of the implementation, which saves you writing the method signatures and correlating them to the interface.

提示:在编写实现时,只要写下“implements MyInterface”并按 Enter 键,Visual Studio 就会创建实现的“骨架”代码,这样您就无需编写方法签名并将它们与接口相关联。

Example code of having two methods with the same name and everythign working well:

具有两个同名方法并且一切正常的示例代码:

Interface MyInterface
    Sub MySub(ByVal arg0 As DateTime)
    Sub MySub(ByVal arg0 As ULong)
End Interface

Class MyImplementation
    Implements MyInterface

    Public Sub MySub(ByVal arg0 As Date) Implements MyInterface.MySub
        ...
    End Sub

    Public Sub MySub(ByVal arg0 As ULong) Implements MyInterface.MySub
        ...
    End Sub
End Class

回答by Robert

You can make the method private and give it another name.

您可以将该方法设为私有并为其命名。

Like:

喜欢:

 Private Sub SaveImpl(ByVal someEntity As IEntity) Implements IRepository.Save

this will look to the outside like: someRepository.Save

这将看起来像: someRepository.Save