Imports System.ComponentModel Public Class ViewModelBase Implements INotifyPropertyChanged #Region "Constructor" Protected Sub New() End Sub #End Region ' Constructor #Region "DisplayName" ''' ''' Returns the user-friendly name of this object. ''' Child classes can set this property to a new value, ''' or override it to determine the value on-demand. ''' Private m_sDisplayName As String Public Overridable Property sDisplayName() As String Get Return m_sDisplayName End Get Protected Set(ByVal value As String) m_sDisplayName = value End Set End Property #End Region ' DisplayName #Region "Debugging Aides" ''' ''' Warns the developer if this object does not have ''' a public property with the specified name. This ''' method does not exist in a Release build. ''' _ Public Sub VerifyPropertyName(ByVal sPropertyName As String) ' Verify that the property name matches a real, ' public, instance property on this object. If TypeDescriptor.GetProperties(Me)(sPropertyName) Is Nothing Then Dim msg As String = "Invalid property name: " & sPropertyName If Me.ThrowOnInvalidPropertyName Then Throw New Exception(msg) Else Debug.Fail(msg) End If End If End Sub ''' ''' Returns whether an exception is thrown, or if a Debug.Fail() is used ''' when an invalid property name is passed to the VerifyPropertyName method. ''' The default value is false, but subclasses used by unit tests might ''' override this property's getter to return true. ''' Private privateThrowOnInvalidPropertyName As Boolean Protected Overridable Property ThrowOnInvalidPropertyName() As Boolean Get Return privateThrowOnInvalidPropertyName End Get Set(ByVal value As Boolean) privateThrowOnInvalidPropertyName = value End Set End Property #End Region ' Debugging Aides #Region "INotifyPropertyChanged Members" ''' ''' Raised when a property on this object has a new value. ''' Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged ''' ''' Raises this object's PropertyChanged event. ''' ''' The property that has a new value. Friend Overridable Sub OnPropertyChanged(ByVal sPropertyName As String) Me.VerifyPropertyName(sPropertyName) If Me.PropertyChangedEvent IsNot Nothing Then RaiseEvent PropertyChanged(Me, New System.ComponentModel.PropertyChangedEventArgs(sPropertyName)) End If End Sub #End Region ' INotifyPropertyChanged Members End Class