Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create an interface in VB.NET with implicit implementations

In C# I can create an interface, and when I use the interface the compiler knows that certain interface requirements are fulfilled by the base class. This is probably clearer with an example:

interface FormInterface
{
    void Hide();
    void Show();
    void SetupForm();
}

public partial class Form1 : Form, FormInterface
{
    public Form1()
    {
        InitializeComponent();
    }

    public void SetupForm()
    {

    }
}

The compiler knows that Hide() and Show() are implemented in Form and the above code compiles just fine. I can't figure out how to do this in VB.NET. When I try:

Public Interface FormInterface
    Sub Hide()
    Sub Show()
    Sub SetupForm()
End Interface


Public Class Form1
    Inherits System.Windows.Forms.Form
    Implements FormInterface

    Public Sub SetupForm() Implements FormInterface.SetupForm

    End Sub

End Class

But the Compiler complains that Form1 must implement 'Sub Hide()' for interface 'FormInterface'. Do I actually have to add the following?

Public Sub Hide1() Implements FormInterface.Hide
    Hide()
End Sub

On all my forms, or is a better route creating an abstract base class that has SetupForm() (and how do you do that in VB.NET)?

like image 581
Kris Erickson Avatar asked Nov 06 '09 07:11

Kris Erickson


People also ask

How do you achieve 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 .

Can an interface have implementations?

All methods of an Interface do not contain implementation (method bodies) as of all versions below Java 8.

WHAT IS interface in VB net explain with example?

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.

Is it mandatory to implement all the methods of an interface?

Yes, it is mandatory to implement all the methods in a class that implements an interface until and unless that class is declared as an abstract class. Implement every method defined by the interface.


1 Answers

No, System.Windows.Forms.Form doesn't have the FormInterface implemented, so VB.NET doesn't know they match. VB.NET doesn't do implicit interface implementation, just explicit.

Yes, you should create a base class and implement your interface on that. I would, however, name them slightly different. Perhaps DoShow and DoHide.

Something like this:

Public Class BaseForm
    Inherits System.Windows.Forms.Form
    Implements FormInterface

    Public Sub SetupForm() Implements FormInterface.SetupForm

    End Sub

    Public Sub DoShow() Implements FormInterface.DoSHow
        Me.Show()
    End Sub

    Public Sub DoHide() Implements FormInterface.DoHide
        Me.Hide()
    End Sub

End Class

Else you could by accident do this:

  Public Class BaseForm
    Inherits System.Windows.Forms.Form
    Implements FormInterface

    Public Sub SetupForm() Implements FormInterface.SetupForm

    End Sub

    Public Sub Show() Implements FormInterface.SHow
        Me.Show()
    End Sub

    Public Sub Hide() Implements FormInterface.Hide
        Me.Hide()
    End Sub

End Class

And that will crash and burn.

Don't make the baseclass MustInherit, because the forms designer won't like that.

like image 112
chrissie1 Avatar answered Nov 04 '22 16:11

chrissie1