34
votes

I am trying to create a class in VB.NET which inherits a base abstract class and also implements an interface. The interface declares a string property called Description. The base class contains a string property called Description. The main class inherits the base class and implements the interface. The existence of the Description property in the base class fulfills the interface requirements. This works fine in C# but causes issues in VB.NET.

First, here is an example of the C# code which works:

public interface IFoo
{
    string Description { get; set; }
}

public abstract class FooBase
{
    public string Description { get; set; }
}

public class MyFoo : FooBase, IFoo
{
}

Now here is the VB.NET version which gives a compiler error:

Public Interface IFoo
    Property Description() As String
End Interface

Public MustInherit Class FooBase
    Private _Description As String
    Public Property Description() As String
        Get
            Return _Description
        End Get
        Set(ByVal value As String)
            _Description = value
        End Set
    End Property
End Class

Public Class MyFoo
    Inherits FooBase
    Implements IFoo
End Class

If I make the base class (FooBase) implement the interface and add the Implements IFoo.Description to the property all is good, but I do not want the base class to implement the interface.

The compiler error is:

Class 'MyFoo' must implement 'Property Description() As String' for interface 'IFoo'. Implementing property must have matching 'ReadOnly' or 'WriteOnly' specifiers.

Can VB.NET not handle this, or do I need to change my syntax somewhere to get this to work?

7
that C# code can't compile because it doesn't implement Description from the abstract class. Are you sure you really want to have an interface AND abstract base?Greg Bogumil
@gbogumil - The C# version does compile - the interface implementation is in the base class.Andrew Hare
The answers below answer your question - personally I find this one of the more annoying things when trying to write clean code in VB.net...Paddy
@Chris - But FooBase will not be implementing this interface, MyFoo will, and it does have the appropriate property via the inheritance. I don't see the duck typing here.Paddy
@300 baud, I wish we were at a bar right now with @Paddy, this would be a fun drunken argument to have! Here's my last on this though, and its just my opinion. If you have a base class of "Fruit" with a property of "Name" and you subclass it with "Apple" and implement "INameable.Name" you are saying "Apple inherits from Fruit which already has a property called Name, I don't really know what it does but just use that to fulfill the contract". Personally I think you should have to do this explicitly, even if its a just a call to "base.Name".Chris Haas

7 Answers

22
votes

You need to mark your property as Overridable or MustOverride in the base class and then you can override it in the child class:

Public MustInherit Class FooBase
    Private _Description As String
    Public Overridable Property Description() As String
        Get
            Return _Description
        End Get
        Set(ByVal value As String)
            _Description = value
        End Set
    End Property
End Class

Public Class MyFoo
    Inherits FooBase
    Implements IFoo
    Public Overrides Property Description() As String Implements IFoo.Description
        Get
            Return MyBase.Description
        End Get
        Set(ByVal value As String)
            MyBase.Description = value
        End Set
    End Property
End Class

Edit This is in response to what @M.A. Hanin posted. Both of our solutions work but its important to understand the ramifications of each. Imagine the following code:

Dim X As FooBase = New MyFoo()
Trace.WriteLine(X.Description)

What comes out of the X.Description? Using the Overridable you'll get the call to the child class while using the Overload method you'll get the call to the base class. Neither is right or wrong, its just important to understand the consequences of the declaration. Using the Overload method you have to up-cast to get the child's implementation:

Trace.WriteLine(DirectCast(X, MyFoo).Description)

If you're just calling MyBase.Description from the child class the question is moot but if you ever change the definition of the child class then you just need to make sure you understand what's going on.

11
votes

One way or the other, you must specify the implementation details of the IFoo interface.

What about this simple option?

Public Class MyFoo
    Inherits FooBase
    Implements IFoo
    Overloads Property Description() As String Implements IFoo.Description
        Get
            Return MyBase.Description
        End Get
        Set(ByVal value As String)
            MyBase.Description = value
        End Set
    End Property
End Class
4
votes

VB requires that the implementing property declare the implementation. This is because of what I actually consider a nice feature of VB that I sometimes miss in C# -- that you can rename the member that implements the interface member.

Thus the only way to make this work without implementing IFoo.Description in FooBase is to declare Description Overridable and then define MyFoo as:

Public Class MyFoo
    Inherits FooBase
    Implements IFoo

    Public Overrides Property Description() As String Implements IFoo.Description
        Get
            Return MyBase.Description
        End Get
        Set(ByVal value As String)
            MyBase.Description = value
        End Set
    End Property
End Class
4
votes

Sorry if I'm late to the party, and apologies too if this functionality has only been introduced in .NET 4, but the following is possible (now)

Public Interface IFoo
    Property Description() As String
End Interface

Public MustInherit Class FooBase
    Implements IFoo

    Public MustOverride Property Description As String Implements IFoo.Description
End Class

Public Class MyFoo
    Inherits FooBase

    Private _description As String

    Public Overrides Property Description As String
        Get
            Return _description
        End Get
        Set(value As String)
            _description = value
        End Set
    End Property
End Class
3
votes

VB.NET does not support implicit implementation. I also ran into this issue and had a lot of trouble.

When you work with generated classes (entities etc.) where you have to explicitly declare Implements IFoo, it makes it impossible at all.

Therefore I submitted a connection to Microsoft and I hope you'll vote and next version of VB they will improve the compiler to be more clever.

1
votes

I can't comment on M.A. Hanin's answer because of my rep, but I would recommend one small tweak to avoid compiler warnings about hiding base methods, assuming you don't want to or can't override override the property in the base class.

Public Class MyFoo
    Inherits FooBase
    Implements IFoo
    Private Property IFoo_Description() As String Implements IFoo.Description
        Get
            Return Me.Description
        End Get
        Set(ByVal value As String)
            Me.Description = value
        End Set
    End Property
End Class
0
votes

This is a strange issue and it clearly shows a difference between the C# and VB.NET compilers. I would suggest that you do implement the interface on the abstract base class as this will make the VB.NET compiler happy and at execution time your child class will still have metadata indicating that it does in fact implement IFoo.

Is there a specific reason that the child class must be the one to declare that it implements the interface?