Star date: 2009.059
Update: I have posted another article here that explains what I think is a better solution to this problem using a simple PostSharp attribute.
When you raise the
PropertyChanged event you have to pass it a property name as a string. If there is no property with that name then nothing will happen. The listener will not be notified and no exception will be thrown making the problem very difficult to debug. You can change this behaviour and make the application fail at runtime by adding a simple check to your helper function for the event:
Public Sub NotifyPropertyChanged(ByVal propertyName As String) ' Throw an exception if the property doesn't exist If Me.GetType().GetProperty(propertyName) Is Nothing Then Throw New InvalidPropertyNameException() End If RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName)) End Sub
If you put this in a base class for all of your model views (or controllers, or presenters) then you will automatically get this functionality every time, preventing some potentially very annoying bugs.
This still isn't the ultimate solution because you don't find out that the property name doesn't exist until runtime. Ideally, we would get a compile error when the property does not exist. What I would like to do is call the function like this:
This way you wouldn't have to use a string at all and the compiler would tell you if
MyProperty doesn't exist. Unfortunately, .NET languages only have delegates for functions/subroutines so there is no way to make a strongly typed pointer to a property. Let's hope they add that in one day, but until then, we'll have to use strings.