question

MarvusMedia avatar image
0 Votes"
MarvusMedia asked XingyuZhao-MSFT commented

Private Shadows does not hide member variable in base class. Can it be done?

I have one base class containing several member variables.

I have four derived classes that inherit the base class.

Two of the variables in the base class are not relevant to one of the four derived classes, so I was hoping to make them unavailable in Intellisense by re-declaring them in the derived class with Private Shadows. However, not only can they still be selected, they actually return the value from the base class! I was not expecting this behaviour at all.

Is there any way to hide irrelevant member variables? This doesn't work:

 Public Class Foo
   Public member1 As String
   Public member2 As Integer
   Public member3 As New List(Of Rectangle)
 End Class
    
 Public Class Bar1
   Inherits Foo
   Private Shadows member2
   ' some code
 End Class
    
 'Elsewhere, in another class:
 Dim b As New Bar1
 Msgbox(b.member2) ' Intellisense let me select it. When run, no error. Msgbox shows 0.
dotnet-visual-basic
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @MarvusMedia ,
The base class defines the properties and behaviors common to subclasses

Two of the variables in the base class are not relevant to one of the four derived classes

In this situation, you can consider separating these two variables from the base class and put them into an interface,
The code looks like:

 Public Class Foo
     Public member1 As String
 End Class
 Interface Foo2
     Property member2 As Integer
 End Interface
    
 Public Class Bar1
     Inherits Foo
    
 End Class
    
 Class Bar2
     Inherits Foo
     Implements Foo2
    
     Public Property member2 As Integer Implements Foo2.member2
 End Class
0 Votes 0 ·

Hi @MarvusMedia ,
Do you still meet this issue? Please let me know if you need further help.

0 Votes 0 ·

0 Answers