Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.NET class inherits a base class and implements an interface issue (works in C#)

Tags:

c#

vb.net

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?

like image 462
300 baud Avatar asked Apr 09 '10 13:04

300 baud


People also ask

Can a class inherit a interface and a class in C#?

Yes, a class can implement an interface that is in a different class as long that the interface is declared as public.

Can we inherit class and interface?

Classes cannot inherit from an interface, since an interface is by definition empty: it only dictates the mandatory implementation of certain members. From the MSDN about interfaces: "An interface contains definitions for a group of related functionalities that a class or a struct can implement."

How do I inherit a class in VB net?

Visual Basic introduces the following class-level statements and modifiers to support inheritance: Inherits statement — Specifies the base class. NotInheritable modifier — Prevents programmers from using the class as a base class. MustInherit modifier — Specifies that the class is intended for use as a base class only.

Can a base class implement an interface?

A base class can also implement interface members by using virtual members. In that case, a derived class can change the interface behavior by overriding the virtual members.


1 Answers

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.

like image 183
Chris Haas Avatar answered Oct 09 '22 10:10

Chris Haas