Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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. This was more a case of the programmer not verifying that the interface in question had not changed from underneath him.

like image 609
Arsalan Ahmed Avatar asked Mar 03 '10 15:03

Arsalan Ahmed


People also ask

How do you achieve interface implementation in VB net?

Implementing Interfaces The Visual Basic reserved word Implements is used in two ways. The Implements statement signifies that a class or structure implements an interface. The Implements keyword signifies that a class member or structure member implements a specific interface member.

How can we avoid implementing all methods interface?

However, you can avoid this by following the below approach: Declare the missing methods abstract in your class. This forces you to declare your class abstract and, as a result, forces you to subclass the class (and implement the missing methods) before you can create any objects.

Can a class implement two interfaces in case they have the same member names C#?

C# allows the implementation of multiple interfaces with the same method name.

How do we perform multiple inheritance in VB net?

Unlike languages that allow multiple inheritance, Visual Basic allows only single inheritance in classes; that is, derived classes can have only one base class. Although multiple inheritance is not allowed in classes, classes can implement multiple interfaces, which can effectively accomplish the same ends.


2 Answers

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.

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
like image 139
M.A. Hanin Avatar answered Sep 18 '22 12:09

M.A. Hanin


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

like image 24
Robert Avatar answered Sep 22 '22 12:09

Robert