How to 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 met by the base class. This is probably more clear 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 code above compiles just fine. I cannot 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 the "FormInterface" interface. Should I add the following?

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

In all my forms, or is it the best route to create an abstract base class that has SetupForm () (and how do you do it in VB.NET)?

+5
2

, System.Windows.Forms.Form FormInterface, VB.NET , . VB.NET , .

, . . , DoShow DoHide.

- :

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

:

  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

.

MustInherit, .

+2

Form Form1, , . , FormInterface IFormWrapper - IFormWrapper, ( #, , , VB):

public class Form1 : IFormWrapper
{
    private readonly Form form;

    public Form1 {
        this.form=new Form();
    }

    public void Show() {
        form.Show();
    }

    public void Hide() {
        form.Hide();
    }

    public void SetupForm() {
        //Code for the setup method
    }
}

, Form1 FormInterface, Form, Show Hide.

0

All Articles