Hiding, but not overiding or replacing, a inherited property

My last thread was locked before I could get my question anwsered, but i realize that the moderater did it because he thought my question had already been anwsered, which is my fault because I did not explain my question clearly; I am a poor writer of English. I know that the forums must be kept clean, so let me refrase my question:

I want to make the Height property not useable from outside the class I inherited from usercontrol, but it is not virtual so I can NOT use the override and browsable(flase) method, I cannot use the new key word because I still need the height property inside of my class. Any suggestions would be much appreciated.



Answer this question

Hiding, but not overiding or replacing, a inherited property

  • vbjunkie

    Yes you can use New .
    Use New to hide the property and set the appropriate properties. When you need to access your Height property use MyBase to access the real Height property

    Private Sub UserControl1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim ActualHeight As Int32 = MyBase.Height
    End Sub
    <System.ComponentModel.Browsable(False)> _
    Private Shadows Property Height() As Int32
    Get
    Height = 0
    End Get
    Set(ByVal value As Int32)

    End Set
    End Property


  • Jeff Weber

    You use the 'shadow' keyword to declare the Height property. Then you can apply browsable(false) attirbute .


  • dbro101

    Right. He seemed more interested in hiding it from the designer. If you wanted to you could just do the following, which merely changes Private to public and then you are caling the method.

    <System.ComponentModel.Browsable(False)> _
    Public Shadows Property Height() As Int32
    Get
    Height = 0
    End Get
    Set(ByVal value As Int32)

    End Set
    End Property


  • Steve98796

    You can't do what you want. You can use the Shadows keyword to "hide" a public base class method; but you can't change the access to it--it will always be public. I.e. if you do the following:


    Public Class Base
    Public Sub Method()
    System.Diagnostics.Debug.WriteLine("Base.Method")
    End Sub
    End Class
    Public Class Derived
    Inherits Base
    Private Shadows Sub Method()
    System.Diagnostics.Debug.WriteLine("Derived.Method")
    End Sub
    End Class

    and performed:


    Dim d As Derived = New Derived
    d.Method

    Base.Method will still be called.



  • MaggieChan

    Basically you want to follow what MarcD said.

    The Height of a UserControl isn't directly accessible anyway, so you don't have to worry about the designer showing that property.  The only thing I would add to the Shadows property is to throw the NotSupportedException when someone tries to set the value of Height outside of your class:

    Public Class UserControl1

     

        Private Sub UserControl1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            MyBase.Height = 250

     

        End Sub

     

        Public Shadows Property Height() As Integer

            Get

                Return MyBase.Height

            End Get

            Set(ByVal value As Integer)

                Throw New System.NotSupportedException("Setting the height of this control is not supported.")

            End Set

        End Property

     

    End Class

     

    This way you give some feedback as to why the height won't change.  You can test this little control to see that the control itself can change it's own size, but if you drop it on a form and try to use the designer to make it taller or shorter, an error message is displayed.



  • Hiding, but not overiding or replacing, a inherited property