1

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?

Calaf
  • 1,133
  • 2
  • 9
  • 22
  • Give [EditorBrowsableAttribute Class](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.editorbrowsableattribute?view=netframework-4.8) a try. I've never been able to make it work, but the documentation describes what you are after – JayV Apr 05 '21 at 11:20
  • Another option would be to use versioned interfaces, like `IExampleV1.P()`, and `IExampleV2.P()`, with each interface redirecting to the appropriate class implementation. This way consumers that need V1 can always get version 1, and so on. New clients always use the latest interface. It's a little more work, but can produce a very nice and functional result. – Bradley Uffner Apr 05 '21 at 11:22
  • Thanks everybody! *EditorBrowsable* solved my problem! – Calaf Apr 05 '21 at 12:47

1 Answers1

2

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.

Sideways
  • 286
  • 2
  • 6
  • Thanks so much! It was just what I was looking for! As for the code hints, of course: This was just an example. – Calaf Apr 05 '21 at 12:46