Programming in C Events CSE 494 R proposed

  • Slides: 12
Download presentation
Programming in C# Events CSE 494 R (proposed course for 459 Programming in C#)

Programming in C# Events CSE 494 R (proposed course for 459 Programming in C#) Prof. Roger Crawfis

The event field An event field is just a special delegate instance. l Usually

The event field An event field is just a special delegate instance. l Usually exposed as a public field (acts more like a property). l Restricts the delegate operations (to the public) to += and -=. l l l Only the class (or its decendents) can fire the event. Only the class (or its descendents) can clear or reset the values (using =).

Published Properties public class Shared. Float { public delegate void New. Value(float value); public

Published Properties public class Shared. Float { public delegate void New. Value(float value); public event New. Value. Changing; public event New. Value. Changed; private void Dummy(float value) {} public Shared. Float() { // This avoids the check for null. Value. Changing += Dummy; Value. Changed += Dummy; } } private float my. Float; public float Value { get { return my. Float; } set { Value. Changing(value); my. Float = value; Value. Changed(my. Float); } }

Example class Model { public event Notifier notify. Views; public void Change() {. .

Example class Model { public event Notifier notify. Views; public void Change() {. . . notify. Views("Model"); } } class View { public View(Model m) { m. notify. Views += new Notifier(Update); } void Update(string sender) { Console. Write. Line(sender + " was changed"); } } class Test { static void Main() { Model model = new Model(); new View(model); . . . model. Change(); } }

Event Accessors Event subscription can be controlled/monitored within a class. l Similar to properties

Event Accessors Event subscription can be controlled/monitored within a class. l Similar to properties – add accessor is called during +=, remove accessor is called during -= l Both accessors need to be declared. l public delegate void My. Delegate (); class A { private My. Delegate m_Delegate. Behind; public event My. Delegate Event { add { m_Delegate. Behind += value; } remove { m_Delegate. Behind -= value; } } … }

Classes with events A class instance may publish several events. l Each event member

Classes with events A class instance may publish several events. l Each event member holds a collection of subscribers. l As with delegates, the publishing object calls . each registered listener in turn. l

Naming Conventions in. NET The name of an event ends with ing if the

Naming Conventions in. NET The name of an event ends with ing if the notification occurs before the state change or actual event. l The name of an event ends with ed if the notification occurs after the state change or actual event. l Some typical event names l l l form. Closing form. Closed msg. Sending msg. Sent // has not closed yet, this can abort the close // by this time the form is closed. // suggests you can intercept and modify // the message just before sending // telling you that it has already gone.

What does this buy us? Flexible, loose coupling Very clean separation of concerns Easily

What does this buy us? Flexible, loose coupling Very clean separation of concerns Easily extensible: we can add new observers without having to modify the publisher. l Modules can be “wired” to listen to one another as part of the startup logic, l The only type coupling between the modules is determined by the type of the event delegate. l l l

Examples in. NET Framework A Timer has a Tick event that you subscribe to.

Examples in. NET Framework A Timer has a Tick event that you subscribe to. You can set the Interval between ticks. l The Event. Log component allows you to listen to Event. Written, which will alert you every time anything is written to your machine’s event log. l The File. System. Watcher component watches a directory structure. It uses a filter (e. g. , “*. xml”) and exposes several events: l l l Changed Created Renamed Deleted

The Event Pattern in. NET l Delegates for event handling in. NET have the

The Event Pattern in. NET l Delegates for event handling in. NET have the following signature: delegate void Some. Event (object sender, My. Event. Args e); l l The return type is void. The first parameter is a reference to the class that contains the event (the publisher). The second parameter is used to pass (push) data to the subscribers. It’s type is derived from System. Event. Args. Using this pattern, I can use a method like the following for all. NET events (using contra-variance): private void My. Event. Handler( object sender, Event. Args e) { // log the event … This method takes any object as the first } parameter and any object derived from Events. Args as the second.

Example public delegate void Key. Event. Handler (object sender, Key. Event. Args e); public

Example public delegate void Key. Event. Handler (object sender, Key. Event. Args e); public class Key. Event. Args : Event. Args { public virtual bool Alt { get {. . . } } // true if Alt key was pressed public virtual bool Shift { get {. . . } } // true if Shift key was pressed public bool Control { get {. . . } } // true if Ctrl key was pressed public bool Handled { get{. . . } set {. . . } } // indicates if event was already handled public int Key. Value { get {. . . } } // the typed keyboard code. . . } class My. Key. Listener { public My. Key. Listener(. . . ) { key. Source. Key. Down += new Key. Event. Handler(Handle. Key); } void Handle. Key (object sender, Key. Event. Args e) {. . . } }

Programming in C# Events CSE 494 R (proposed course for 459 Programming in C#)

Programming in C# Events CSE 494 R (proposed course for 459 Programming in C#) Prof. Roger Crawfis