Created
February 25, 2020 12:53
-
-
Save karenpayneoregon/d574a9da2849a3dcb488002892f590bc to your computer and use it in GitHub Desktop.
Simple example for interface, INotifyPropertyChanged, data binding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Imports System.ComponentModel | |
Imports System.Runtime.CompilerServices | |
''' <summary> | |
''' Requires two ListBox controls, ListBox1, ListBox2 | |
''' | |
''' Shows a simple use of a Interface and a simple use | |
''' of a BindingList. Note by implementing INotifyPropertyChanged and a BindingList | |
''' we can update a ListBox while without the BindingList the change is not | |
''' reflected in the second listbox. | |
''' | |
''' All classes are in one file to make it easy to try. | |
''' </summary> | |
Public Class Form1 | |
Private ReadOnly _people As New List(Of Person) From { | |
New Person() With {.Id = 1, .FirstName = "Karen", .LastName = "Payne"}, | |
New Person() With {.Id = 2, .FirstName = "Jean", .LastName = "Payne"}} | |
Private ReadOnly _managers As New List(Of Manager) From { | |
New Manager() With {.Id = 1, .FirstName = "Mike", .LastName = "Jones"}, | |
New Manager() With {.Id = 2, .FirstName = "Bill", .LastName = "Smith"}} | |
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load | |
Dim firstPerson = CType(_people.FirstOrDefault(), IPerson) | |
Dim firstManager = CType(_managers.FirstOrDefault(), IPerson) | |
Console.WriteLine($"Person: {firstPerson.FirstName}") | |
Console.WriteLine($"Manager: {firstManager.FirstName}") | |
ListBox1.DataSource = New BindingList(Of Person)(_people) | |
ListBox2.DataSource = _people | |
CType(ListBox1.SelectedItem, Person).FirstName = "Mary" | |
CType(ListBox2.SelectedItem, Person).FirstName = "Mary" | |
' Karen shows in the list box but the actual value is Mary | |
Console.WriteLine(_people.FirstOrDefault().FirstName) | |
End Sub | |
End Class | |
Public Interface IPerson | |
Property Id() As Integer | |
Property FirstName() As String | |
Property LastName() As String | |
End Interface | |
Public Class Person | |
Implements INotifyPropertyChanged, IPerson | |
Private _firstName As String | |
Private _lastName As String | |
Public Property Id As Integer Implements IPerson.Id | |
Public Property FirstName() As String Implements IPerson.FirstName | |
Get | |
Return _firstName | |
End Get | |
Set | |
_firstName = Value | |
OnPropertyChanged() | |
End Set | |
End Property | |
Public Property LastName() As String Implements IPerson.LastName | |
Get | |
Return _lastName | |
End Get | |
Set | |
_lastName = Value | |
OnPropertyChanged() | |
End Set | |
End Property | |
Public Overrides Function ToString() As String | |
Return $"{FirstName} {LastName}" | |
End Function | |
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged | |
Protected Overridable Sub OnPropertyChanged(<CallerMemberName> Optional ByVal propertyName As String = Nothing) | |
PropertyChangedEvent?.Invoke(Me, New PropertyChangedEventArgs(propertyName)) | |
End Sub | |
End Class | |
Public Class Manager | |
Implements INotifyPropertyChanged, IPerson | |
Private _firstName As String | |
Private _lastName As String | |
Public Property Id As Integer Implements IPerson.Id | |
Public Property FirstName() As String Implements IPerson.FirstName | |
Get | |
Return _firstName | |
End Get | |
Set | |
_firstName = Value | |
OnPropertyChanged() | |
End Set | |
End Property | |
Public Property LastName() As String Implements IPerson.LastName | |
Get | |
Return _lastName | |
End Get | |
Set | |
_lastName = Value | |
OnPropertyChanged() | |
End Set | |
End Property | |
Public Overrides Function ToString() As String | |
Return $"{FirstName} {LastName}" | |
End Function | |
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged | |
Protected Overridable Sub OnPropertyChanged(<CallerMemberName> Optional ByVal propertyName As String = Nothing) | |
PropertyChangedEvent?.Invoke(Me, New PropertyChangedEventArgs(propertyName)) | |
End Sub | |
End Class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment