Events

Events

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.

Lab

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:

  1. Add an event named Changed of type NotifyPropertyChanged to the interface Payable
  2. Add the event also to the class HumanResource
  3. In HumanResource, there is a protected method named FireChangedEvent
  4. fill in the body of this method to raise the event (remember check for null)
  5. If Id, Name, or Salary is changed, call FireChangedEvent
  6. 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.

Complete and Continue  
Discussion

0 comments