Events in C# provide a convenient way to implement inversion of control (IoC) patterns like publish-subscribe. The keyword event as well as += and -= operators along with delegates provide the eventing mechanism. Check out the video lecture and the lab that follows!
Note - the file EventSamples.zip below contains the sample code used in the lecture.
In this lab, we will implement an event that notifies subscribers when a property's value is changed.
Download, unzip and open the project from the zip file below. Review Payable.cs. A delegate named NotifyPropertyChanged has already been defined.
Follow these steps to complete the exercise:
- Add an event named Changed of type NotifyPropertyChanged to the interface Payable
- Add the event also to the class HumanResource
- In HumanResource, there is a protected method named FireChangedEvent
- fill in the body of this method to raise the event (remember check for null)
- If Id, Name, or Salary is changed, call FireChangedEvent
- Salary is in the Employee class
Note - the project will not build until step one is completed
If you need a little help, check out the zip below.