Imports System.Runtime.CompilerServices
Imports System.ComponentModel
Imports System.Collections.Generic
Public Module Exts
<Extension> Public Sub SetProperty(Of T, U As INotifyPropertyChanging_Extd)(ByVal obj As U,
[property] As String,
toValue As T,
ByRef usingField As T, Optional ignored As Object = Nothing)
If obj Is Nothing Then Exit Sub
If Not EqualityComparer(Of T).Default.Equals(usingField, toValue) Then
Dim old = usingField
obj.RaisePropertyChangingEvent(obj, New PropertyChangingEventArgs([property]))
usingField = toValue
obj.RaisePropertyChangedEvent(obj, New PropertyChangedEventArgs([property]))
End If
End Sub
<Extension> Public Sub SetProperty(Of T, U As INotifyPropertyChanged_Extd)(ByVal obj As U,
[property] As String,
toValue As T,
ByRef usingField As T)
If obj Is Nothing Then Exit Sub
If Not EqualityComparer(Of T).Default.Equals(usingField, toValue) Then
usingField = toValue
obj.RaisePropertyChangedEvent(obj, New PropertyChangedEventArgs([property]))
End If
End Sub
End Module

Example Class using this extension method.

Public Class Example
Implements INotifyPropertyChanged_Extd
Private _Value As Integer
Property Value As Integer
Get
Return _Value
End Get
Set(value As Integer)
Me.SetProperty("Value", value, _Value)
End Set
End Property
Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
Public Sub RaisePropertyChangedEvent(source As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged_Extd.RaisePropertyChangedEvent
RaiseEvent PropertyChanged(source, e)
End Sub
End Class

In terms of visual density, this was partly due to the fact that Anders codes using K&R style bracing instead of Allman, as well as the extra code he uses to properly avoid the race condition that exists in the MSDN example. I updated the blog post to use Allman style for both.

The other thing I did want to point out was that there is at least one other advantage besides just reductions in raw lines of boiler plate code in property setter, which is that it sets up a Pit of Success for properly implementing INotifyPropertyChanged in subclasses.

I work on a very large WPF/C# enterprise project at the moment and there are literally *thousands* of incorrectly implemented properties in the application that need to be fixed to properly check for inequality before setting the field and raising the event.

I started watching this podcast on my bus ride into work today. Rick, you used a term I've not heard before, called "New Developers". I think you said it is a term that was introduced at the Build conference. I wasn't able to make the Build conference, so would you please define what that term means.

Hi Rod, Sorry for the delay. I was heads down in code and just got out. I said "New Developer" but the phrase that was introduced was actually "modern developer". I think that one is more accurate, but still very amorphous right now. At the core, would be the "traditional" seasoned developer, tackling the big, hairy enterprise level projects and master weilder of all things COM or .NET. That's really the foundation. But we're entering a time where building on top of that are a vast army of web developers, Javascript masters, interaction developers, designers who code, scripters, etc. In Windows 8, we're seeing an introductory level playing field between C++, Javascript and C#. Canvas and HTML5 are blowing up, and DirectX 11.1 becoming more accessible through the OS is going to also create a lot of great opportunities for everyone up and down the stack. From Designers who code all the way down to enterprise dev super stars. From Phone to Hardware to Cloud, from browser based scripts all the way down to running native on the metal and GPU. I think of "modern developer" as being representitive of this entire spectrum, and that's how I was using it. A sign of the massive opportunities coming into the Microsoft ecosystem. Hope that helps. - rk

@rbarraza: Rick, it's my turn to apologize for not responding sooner. WOW, almost a month after you replied to me. I'm sorry!

Thank you for your description as to what you mean by a "new developer". It's interesting, then, as it now seems like Microsoft is going to make it possible for the hobbiest developer to get back into the game, so to speak. I know that you meant more than that, but it seems as though the new developer would be someone who isn't necessarily a heavy duty enterprise developer (although they're included), but could also be the person who writes a few scripts on the side, occasionally dabbles in HTML and JavaScript, that Windows 8 will open it up for people like that again. Something that was lost with each new version of VS.