How to implement explicit interface implementation in VB.NET?
To declare the implementation of an interface method, you can use any attributes that are legal on instance method declarations, including Overloads , Overrides , Overridable , Public , Private , Protected , Friend , Protected Friend , MustOverride , Default , and Static .
An implicit interface implementation is where you have a method with the same signature of the interface. An explicit interface implementation is where you explicitly declare which interface the method belongs to.
However, classes that implement an interface do contain code. Keep in mind that there are no instances of interfaces in VB . NET. Every instance is a type that implements an interface, but is itself not an instance of the interface.
VB.NET Interfaces So, an interface is nothing but a collection of method and property declarations. An interface can declare only a group of related functionalities, it is the responsibility of the deriving class to implement that functionality. An interface is defined with the Interface keyword.
As others have said, make the routine private, and it forces clients to call through the interfaces.
Just one more word. Explicit interface implementation in C# also allows the programmer to inherit two interfaces that share the same member names and give each interface member a separate implementation. In VB.Net, that's easy, because the implementing methods can have different names from the interface names.
Function ICanUseAnyNameILike() As String Implements IFoo.Bar
Function ItsTotalAnarchy() As String Implements IFoo2.Bar, IFoo3.ItsMadnessReally
Just declare sub/function Private
:
Private Function Bar() As String Implements IFoo.Bar
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With