Converting inheritance with an interface from C # to VB.NET

How to convert the following code to VB.NET?

class A { public int NumberA { get; set; } } class B : A, I { public int NumberB { get; set; } } interface I { int NumberA { get; set; } int NumberB { get; set; } } 

In VB.NET, there is a problem with the "Implemented" keyword after declaring a property. So I need to do something like this:

 Class B Inherits A Implements IC Public Property NumberB() As Integer Implements IC.NumberB Get Return m_NumberB End Get Set(ByVal value As Integer) m_NumberB = value End Set End Property Private m_NumberB As Integer Public Property NumberA1() As Integer Implements IC.NumberA Get Return MyBase.NumberA End Get Set(ByVal value As Integer) MyBase.NumberA = value End Set End Property End Class 

But there is a duplicate input property of NumberA1. Is there any way to make this smarter?

+4
source share
3 answers

Yes, it is not supported in vb.net. The compiler requires the interface to be implemented with the explicit keyword Implements, unlike C #. The closest you can get is:

 Interface ICA Property NumberA() As Integer End Interface Interface ICB Property NumberB() As Integer End Interface Class A Implements ICA Public Property NumberA() As Integer Implements ICA.NumberA '' etc End Property End Class Class B Inherits A Implements ICA, ICB Public Property NumberB() As Integer Implements ICB.NumberB '' etc... End Property End Class 

Note that B no longer needs to implement NumberA, although it implements ICA, and the implementation in is accepted. But, of course, this requires too many redrafted initial definitions. The workaround you used is good and has no harmful effects at runtime. Please note that you can make A1 private.

+1
source

You can force the shading of an old property from your class using the Shadows keywords.

So maybe this is normal for you:

 Public Class A Public Property NumberA As Integer End Class Public Class B Inherits A Implements I Public Shadows Property NumberA As Integer Implements I.NumberA Get Return MyBase.NumberA End Get Set(ByVal value As Integer) MyBase.NumberA = value End Set End Property Public Property NumberB As Integer Implements I.NumberB End Class Interface I Property NumberA As Integer Property NumberB As Integer End Interface 
0
source

You can also have a second interface that inherits from the first:

 Interface IA Property NumberA() As Integer End Interface Interface IB Inherits IA Property NumberB() As Integer End Interface Class A Implements IA Public Property NumberA() As Integer Implements IA.NumberA Get Return m_NumberA End Get Set(ByVal value As Integer) m_NumberA = value End Set End Property Private m_NumberA As Integer End Class Class B Inherits A Implements IB Public Property NumberB() As Integer Implements IB.NumberB Get Return m_NumberB End Get Set(ByVal value As Integer) m_NumberB = value End Set End Property Private m_NumberB As Integer End Class 
0
source

Source: https://habr.com/ru/post/1389625/


All Articles