1

I have an interface defined in C# project:

public interface IForm
{
    bool IsDisposed { get; }
    void Show();
}

I implemented it in a C# class in WinForms

public partial class Form1 : Form, IForm {  }

As the method Show() and property IsDisposed are already present in the System.Windows.Forms.Form class I do not require to implement it.

But same doesn't hold true in VB.NET, in VB.NET I have to define the members, otherwise getting compile error.

Public Class Form1
    Implements WindowsFormsApplication1.IForm

    Public ReadOnly Property IsDisposed1 As Boolean Implements WindowsFormsApplication1.IForm.IsDisposed
        Get

        End Get
    End Property

    Public Sub Show1() Implements WindowsFormsApplication1.IForm.Show

    End Sub
End Class

Why do I have to define the interface members in VB.NET if they are already present in the base class ?

5
  • 4
    I don't see where your VB.NET class is inheriting from System.Windows.Form. Commented Apr 19, 2013 at 12:53
  • Two languages have different rules. This isn't surprising. If they were identical in all respects, they wouldn't be two languages. Commented Apr 19, 2013 at 12:58
  • 3
    @Oded: I guess he created the form with the designer (or morelikely, it was the one that is there when you create a new vb application), which means the Inherits Form statement is in the Form1.Designer.vb file. Commented Apr 19, 2013 at 13:12
  • @Pondidum, you are right. Commented Apr 19, 2013 at 14:51
  • One question. Doesn't the above VB code mean that we replace the default implementations of the Show and IsDisposed members in the Form class with empty procedures (IsDisposed1, Show1)? In other words, now the Show and IsDisposed members of the Form1 simply do nothing. Commented Jan 19, 2017 at 9:20

3 Answers 3

8

VB does not have implicit interface implementation, only explicit while C# supports both.

This means that you have to explicitly say exactly what member that implements an interface member. This adds some flexibility, for example you can make the method that implements an interface member private or protected and you can give it a name that differs from the interface member.

You can read more about the details of this here: http://ondevelopment.blogspot.se/2008/10/implementing-interfaces-in-vbnet.html

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

1 Comment

On the flipside, vb.net allows a method which implements an interface to have a visibility other than "public" or "none". IMHO, in an inheritable class, if the visibility isn't "public" it should more often than not "protected" [typically using a slightly-modified name], since derived classes will often need to modify a method's behavior without replacing it entirely. The way C# does things, a parent class's explicit implementation of an interface cannot be used from within a child class's implementation.
2

VB.NET allows you to name a function/sub differently than the function/sub that it implements. This means that you must explicitly add the Implements <Function/Sub> to the end of the signature.

In C# you can't do this, so the implementations "just work" without you having to add anything.

MSDN:

The parameter types and return types of the implementing member must match the interface property or member declaration in the interface. The most common way to implement an element of an interface is with a member that has the same name as the interface

1 Comment

You can implement a interface explicitly in C#, the Method is given the full InterfaceName.MethodName syntax
1

Use the shadows keyword if you want to override the standard methods of the Form and replace them with the ones defined in your interface otherwise you are required to use a different name as they are treated as two separate methods.

Public Class Form1
    Inherits Form
    Implements IForm

    Public Shadows Property IsDisposed As Boolean Implements IForm.IsDisposed

    Public Shadows Sub Show() Implements IForm.Show
        ' replaces original method in Form class
    End Sub

End Class

Alternative:

Public Class Form2
    Inherits Form
    Implements IForm

    Public Property IsDisposed1 As Boolean Implements IForm.IsDisposed

    Public Sub Show1() Implements IForm.Show
        Me.Show() ' Original method still exists and is accessible like this
    End Sub
End Class

1 Comment

I really wish that ShowDialog, Show and Close were marked as Overrideble for this reason, I do hate shadowing methods :(

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.