views:

222

answers:

3

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 implimented 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

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  A: 

This is by design. Maybe that's one area where C# is better than VB.NET.

Darin Dimitrov
+1 - this didn't deserve -1. That's a good link: a blog entry by the program manager of VB.net, that explains the difference between explicit interface implementation (in VB.net) and implicit interface implementation (in C#) with pros and cons, including the area raised in this question. There are advantages to both approaches - the VB.Net method means you don't have to do things like this http://msdn.microsoft.com/en-us/library/aa288461%28VS.71%29.aspx
MarkJ
A: 

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.

Konamiman
And the downvote is because...?
Konamiman
+1  A: 

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

Yes you should create a baseclass 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.

chrissie1