当两个方法具有相同名称但参数不同时,如何在VB.Net中实现接口 [英] How to implement an interface in VB.Net when two methods have the same name but different parameters

查看:154
本文介绍了当两个方法具有相同名称但参数不同时,如何在VB.Net中实现接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

N.B。这更像是一个程序员没有验证有问题的界面没有从他下面改变的情况。

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

推荐答案

这怎么会让编译器感到困惑?
编译器期望找到每个方法签名的实现,并通过签名区分实现。

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.

提示:编写实现时,一旦你写下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

这篇关于当两个方法具有相同名称但参数不同时,如何在VB.Net中实现接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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