6

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)?

1
  • @Kris Erickson: I don't get this "implicit implementation" thing... Anyway, I guess interfaces have to be completely implemented as per opposition to base classes. An interface forces its implementations into a derived class. If your only want to add these methods, I perhaps would consider a base abstract class (MustInherit in VBNET). Is this accurate enough for your needs? Commented Feb 10, 2010 at 14:55

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Unless you intended to use protected members of the Form class in Form1 members, I would use a containment relationship instead of inheritance. So you would have your FormInterface named something like IFormWrapper instead, and your implementation would be like this (I show it in C# because it is my working language, I think you will be able to translate the idea to 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
    }
}

After all, if you were planning to use Form1 instances through the FormInterface class, you would have had no access to the Form members other than Show and Hide.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.