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

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

NB This was more likely the case when the programmer did not check that the interface in question did not change from under it.

+6
interface implementation
source share
2 answers

How does this confuse the compiler? The compiler expects to find an implementation for each method signature and distinguishes implementations by their signatures.

If the signatures are identical / indistinguishable (in most cases this means that the arguments are of the same type in the same order), you will get a development-time error related to the interface, stating that these two methods cannot overload each other because they have the same signature.

So, in any case, the compiler should not be confused. If you need more help, attach a sample code - these things are relatively easy to resolve.

Tip. When writing an implementation, as soon as you write “implements MyInterface” and press Enter, Visual Studio will create a “skeletal” implementation code, which saves you from writing method signatures and correlating them with the interface.

Sample code in two ways with the same name and all functions:

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 
+9
source share

You can make the method private and give it a different name.

Like:

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

it will look like external: someRepository.Save

+1
source share

All Articles