Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

How to: Implement the INotifyPropertyChanged Interface

The following code example demonstrates how to implement the INotifyPropertyChanged interface. Implement this interface on business objects that are used in Windows Forms data binding. When implemented, the interface communicates to a bound control the property changes on a business object.

' This class implements a simple customer type  
' that implements the IPropertyChange interface. 

Public Class DemoCustomer
    Implements INotifyPropertyChanged

    ' These fields hold the values for the public properties. 
    Private idValue As Guid = Guid.NewGuid()
    Private customerName As String = String.Empty
    Private companyNameValue As String = String.Empty
    Private phoneNumberValue As String = String.Empty

    Public Event PropertyChanged As PropertyChangedEventHandler _
      Implements INotifyPropertyChanged.PropertyChanged

    Private Sub NotifyPropertyChanged(ByVal info As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
    End Sub 

    ' The constructor is private to enforce the factory pattern. 
    Private Sub New() 
        customerName = "no data"
        companyNameValue = "no data"
        phoneNumberValue = "no data" 

    End Sub 'New 

    ' This is the public factory method. 
    Public Shared Function CreateNewCustomer() As DemoCustomer 
        Return New DemoCustomer()

    End Function 

    ' This property represents an ID, suitable 
    ' for use as a primary key in a database. 
    Public ReadOnly Property ID() As Guid
            Return Me.idValue
        End Get 
    End Property 

    Public Property CompanyName() As String  
            Return Me.companyNameValue
        End Get  
            If value <> Me.companyNameValue Then 
                Me.companyNameValue = value
            End If 
        End Set 
    End Property 

    Public Property PhoneNumber() As String  
            Return Me.phoneNumberValue
        End Get  
            If value <> Me.phoneNumberValue Then 
                Me.phoneNumberValue = value
            End If 
        End Set 
    End Property 
End Class

To compile the previous code example:

  • Paste the code into an empty code file. Use the business object in a Windows Forms application that contains a Main method.

Community Additions

© 2015 Microsoft