Search code examples
vb.netbrowsable

How to hide a public property without making it private or removing it


Premise

Suppose that we have a class in a dll with a simple public property "OldP". In a newer version of this dll, I want to change the name of this property in "NewP". I can't simply replace the property name, because this dll is used in a lot of program and I can't change them... I want to hide this property and show only the newer property. How is this possible?

"Browsable" attempt

Before asking this, I searched similar questions on StackOverflow and read something about Browsable, but it doesn't work. Here is my code:

Public Class Example

    Private _p As Integer

    <System.Obsolete("Use NewP instead", False)>
    <System.ComponentModel.Browsable(False)>
    Public Property OldP() As Integer
        Get
            Return _p
        End Get
        Set(ByVal value As Integer)
            _p = value
        End Set
    End Property


    Public Property NewP() As Integer
        Get
            Return _p
        End Get
        Set(ByVal value As Integer)
            _p = value
        End Set
    End Property

End Class

When I create a new Example object, I can still see "OldP":

Output

Where I'm wrong?


Solution

  • You cannot completely prevent the old property from being used, but you can hide it from IntelliSense by adding this directive: <System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)>. The OldP property will no longer be suggested, but still be accessible normally.

    Beside that, I recommend you change the OldP property so that it refers not to the _p object, but to the NewP property to make future management easier, so that you don't have to worry about the deprecated version in the future.