Explicit Implementation

To implement an explicit interface, remove the public access specifier and prefix the method with the name of the interface
Adding explicit interface implementation can be used when you do not want to expose those methods on the interface as part of the class's methods.
When a member is explicitly implemented it cannot be accessed through the class instance, only through the instance of the interface.

In an interface all methods are public by definition so you must exclude the public access specifier.
If you include a "public" modifier you will get a "The modifier public is not valid for this item".

public interface IMyInterface2 
   void string MyMethod2(string arg2);
public class MyClass2 : IMyInterface2
   void IMyInterface2.MyMethod2(string arg2)

This particular example does not work inside a windows form ?
Explicit interface implementation can also be used to achieve the following:
1) When you want to change the return type. If a method returns an object but you want to implement it returning a string
2) different implementation when called directly from the class ?

Explicit Interface Implementation (VB.Net)

In VB.Net you must explicitly tell the compiler that you want to implement an interface member by using the Implements keyword.
Creating a method with the same name, signature and return type does not achieve this
There is no implicit interface implementation in VB.Net.
Once advantage of this explicit implementation is that you have the ability to give the method a difference name.

Public Interface IMyInterface2 
   Sub MyMethod2()
End Interface

Public Class MyClass2
   Implements IMyInterface2

   Sub MyMethod2() Implements IMyInterface2.MyMethod2
   End Sub

End Class

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext