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

后端 未结 7 1683
后悔当初
后悔当初 2020-12-09 14:53

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. T

相关标签:
7条回答
  • 2020-12-09 15:26

    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?

    0 讨论(0)
  • 2020-12-09 15:27

    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
    
    0 讨论(0)
  • 2020-12-09 15:29

    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
    
    0 讨论(0)
  • 2020-12-09 15:29

    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
    
    0 讨论(0)
  • 2020-12-09 15:37

    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.

    0 讨论(0)
  • 2020-12-09 15:39

    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 讨论(0)
提交回复
热议问题