I suggest you ...

Add NotifyPropertyChanged attribute (or keyword) to properties declared as auto-implemented properties

I absolutely love using auto-implemented properties and the removal of the need to declare a backing variable.

Public Property MyProperty As String

However, for classes that implement INotifyPropertyChanged, I can't use auto-implemented properties, and instead have to write the more verbose:

Private _myProperty As String
Public Property MyProperty As String
Get
Return _myProperty
End Get
Set(value As String)
_myProperty = value
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("MyProperty"))
End Set
End Property

Multiply this by a few dozen properties, and this gets a little tedious (even with code snippets), much too verbose to read, and increases the surface area for typo bugs.

I am unable to write my own NotifyPropertyChangedAttribute class, because I cannot directly hook into the internal workings of the implemented property.

I would like to suggest the following syntax:

<NotifyPropertyChanged> Public Property MyProperty As String

...or perhaps add a keyword to the language itself, as follows:

Public Notify Property MyProperty As String

(It would be nice if the compiler, upon seeing the Notify keyword, automatically sets the class to implement INotifyPropertyChanged underneath the syntactic sugar.)

Of course, for more complex implementations where setting one property changes another calculated readonly property, then we'd have to revert to writing the property in longhand.

33 votes
Vote
Sign in
(thinking…)
Sign in with: facebook google
Signed in as (Sign out)
You have left! (?) (thinking…)
Mike C shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

Thanks for taking the time to share this suggestion. This item has been around for a couple of versions of Visual Studio and we haven’t acted on it. Looking at the VS “15” plans, we’re not going to take action on this item, so we’re going to close it. If the suggestion is still relevant, please either take a look to see if there’s another suggestion that’s similar that you can vote on, or open a new suggestion.

- The Visual Studio Team

2 comments

Sign in
(thinking…)
Sign in with: facebook google
Signed in as (Sign out)
Submitting...
  • HMan commented  ·   ·  Flag as inappropriate

    A thousand votes on this. CallerMemberNameAttribute helped the situation immensely in .NET 4.5, but there really is no reason at all to have to manually implement properties just to have them notify. Add a NotifyPropertyChangeAttribute, for the love of god. So many wasted keystrokes!

  • David Day commented  ·   ·  Flag as inappropriate

    I voted for this, however... you have to define when a property changes. For example, whenever I raise my events I use this:

    Private _Foo As String
    Public Property Foo() As String
    Get
    Return _Foo
    End Get
    Set(ByVal value As String)
    If value <> _Foo Then
    _Foo = value
    End If
    End Set
    End Property

    Protected Sub OnFooChanged()
    RaiseEvent FooChanged(Me, EventArgs.Empty)
    End Sub

    Public Event FooChanged(ByVal sender As Object, ByVal e As EventArgs)

    The reason for this is because if the value is the same as the variable, then the property did not really change. However, some things do not use the <> or = sign to check for equality, rather they use the Is keyword. Therefore some things would have issues with this.

Feedback and Knowledge Base