Chapter-7 S. NandaGopalan, BIT Delegates and Events Chapter-7 S. NandaGopalan, BIT
Chapter-7 S. NandaGopalan, BIT Chapter Objectives .NET Delegate Types – Basic Concepts How to build a C# Delegate – Example Multicasting with Delegates Asynchronous Delegates Events – Basic Concepts Listening to incoming Events Examples on Events Chapter-7 S. NandaGopalan, BIT
.NET Delegate Types - Basic Concepts Delegates, Interfaces and Events allow you to provide callback functionality. Each type has its own specific usage characteristics that make it better suited to particular situations. A delegate in C# is similar to a function pointer in C or C++ Delegate is an object that points to another method in the application Delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method All these happen at run time and not at compile time Unlike function pointers in C or C++, delegates are object-oriented, type-safe, and secure Chapter-7 S. NandaGopalan, BIT
Chapter-7 S. NandaGopalan, BIT Basic Concepts… A delegate declaration defines a type that encapsulates name of the method a set of arguments a return type For static methods, a delegate object encapsulates the method to be called For instance methods, a delegate object encapsulates both an instance and a method on the instance If you have a delegate object and an appropriate set of arguments, you can invoke the delegate with the arguments at run time Chapter-7 S. NandaGopalan, BIT
Chapter-7 S. NandaGopalan, BIT Basic Concepts… An interesting and useful property of a delegate is that it does not know or care about the class of the object that it references any object will do all that matters is that the method's argument types and return type match the delegate's This makes delegates perfectly suited for "anonymous" invocation Chapter-7 S. NandaGopalan, BIT
Delegates vs. Interfaces Delegates and interfaces are similar in that they enable the separation of specification and implementation Delegate specifies the signature of a method, and authors can write methods that are compatible with the delegate specification. Same is true even in the case of an interface when should you use delegate/interface? Chapter-7 S. NandaGopalan, BIT
Chapter-7 S. NandaGopalan, BIT Use Delegates when… A single method is being called A class may want to have multiple implementations of the method specification It is desirable to allow using a static method to implement the specification An event-like design pattern is desired The caller has no need to know or obtain the object that the method is defined on The provider of the implementation wants to "hand out" the implementation of the specification to only a few select components Easy composition is desired Chapter-7 S. NandaGopalan, BIT
Chapter-7 S. NandaGopalan, BIT Use Interfaces when… The specification defines a set of related methods that will be called A class typically implements the specification only once The caller of the interface wants to cast to or from the interface type to obtain other interfaces or classes Chapter-7 S. NandaGopalan, BIT
Chapter-7 S. NandaGopalan, BIT Delegate in C# To declare a delegate, use the keyword delegate public delegate void ProcessBookDelegate(Book book); C# automatically creates a new sealed class with three methods: sealed class ProcessBookDelegate : system.MulticastDelegate { public ProcessBookDelegate (object target, uint functionAddress); public void Invoke (Book book); public IAsyncResult BeginInvoke (Book book); public void EndInvoke (IAsyncResult result); } Chapter-7 S. NandaGopalan, BIT
System.MulticastDelegate A multicast delegate is one that can have more than one element in its invocation list. This means, a multicast delegate can point to multiple methods When a multicast delegate is invoked, the delegates in the invocation linked list are called synchronously in the order in which they appear Method - returns the name of the static method Target - gets the class instance on which the current delegate invokes the instance method Combine() – adds a method to the list GetInvocationList() – returns an array of System.Delegate types Remove() – removes a delegate from the list Chapter-7 S. NandaGopalan, BIT
Example 1 – Simple: Outline AnyMethodTakingAString(string s) delegate represents an object that maintains a reference to some method that has a string type parameter and returns void When you want to call the method thro' the delegate, pass the name of the method to the delegates constructor AnyMethodTakingAString del; del = new AnyMethodTakingAString(PlainPrint); del("BIT"); Now PlainPrint() method will be invoked via the delegate Chapter-7 S. NandaGopalan, BIT
Chapter-7 S. NandaGopalan, BIT Example 1 - Simple class DelegateApp { // This is the method that will be called by the delegate. public static void PlainPrint(string msg) { Console.WriteLine("Msg is: {0}", msg); } public static void UpperCasePrint(string msg) { Console.WriteLine("Msg is: {0}", msg.ToUpper()); } public static void XXXXYYYYZZZZ888777aaa(string msg) { Console.WriteLine("Msg is: {0}", msg); } // Bad target! Why? public void BadTargetForDelegate(int x, AppDomain y) { // Stuff. } // Define a delegate. public delegate void AnyMethodTakingAString(string s); Chapter-7 S. NandaGopalan, BIT
Chapter-7 S. NandaGopalan, BIT Main() public static void Main() { Console.WriteLine("***** A very simple delegate example *****\n"); // Make the delegate. AnyMethodTakingAString del; del = new AnyMethodTakingAString(PlainPrint); del("Hello there..."); Console.WriteLine("->I just called: {0}\n", del.Method); // Reassign and invoke delegate. del = new AnyMethodTakingAString(UpperCasePrint); del = new AnyMethodTakingAString(XXXXYYYYZZZZ888777aaa); } Chapter-7 S. NandaGopalan, BIT
Example - 2 (Sorting) DelegateTest a – int array bool AscSort(e1, e2) bool DescSort(e1, e2) DelegateBSort.bubbleSort(a, new DelegateBSort.Comp(AscSort)) DelegateBSort.bubbleSort(a, new DelegateBSort.Comp(DescSort)) DelegateBSort delegate bool Comp(e1, e2) static void bubbleSort(a, Comp c) Chapter-7 S. NandaGopalan, BIT
Chapter-7 S. NandaGopalan, BIT Sorting… public class DelegateBSort { public delegate bool Comp(int e1, int e2); public static void Swap(ref int x, ref int y) int t; t = x; x = y; y = t; } public static void bubbleSort(int[ ] a, Comp c) for(int i = 0; i < a.Length - 1; i++) for(int j = 0; j < a.Length - 1 - i; j++) if (c(a[ j ], a[ j+1 ])) Swap(ref a[ j ], ref a[ j+1 ]); Chapter-7 S. NandaGopalan, BIT
Chapter-7 S. NandaGopalan, BIT public class DelegateTest { private int[ ] a = new int[4] {10, 40, 20, 30}; // method to be called for ascending order public bool AscSort(int e1, int e2) { return e1 > e2; } // method to be called for descending order public bool DescSort(int e1, int e2) { return e1 < e2; } public void DispAscArr( ) // Delegate the sorting to ascending order DelegateBSort.bubbleSort(a, new DelegateBSort.Comp(AscSort)); for (int i = 0; i < a.Length ; i++) Console.WriteLine(a[i]); } public void DispDescArr( ) // Delegate the sorting to descending order DelegateBSort.bubbleSort(a, new DelegateBSort.Comp(DescSort)); Chapter-7 S. NandaGopalan, BIT
Chapter-7 S. NandaGopalan, BIT Main() public static void Main(string[ ] args) { DelegateTest obj = new DelegateTest ( ); Console.WriteLine("Ascending Array: "); obj.DispAscArr ( ); Console.WriteLine("Descending Array: "); obj.DispDescArr ( ); } Chapter-7 S. NandaGopalan, BIT
Example - 3 (Abstract Model) Main( ) g.ProcessCars(new Car.CarDelegate(WashCar) ) static void WashCar(Car c) static void RotateTires(Car c) Garage theCars - ArrayList void ProcessCars(Car.CarDelegate proc) Car isDirty shouldRotate delegate void CarDelegate (Car c) Chapter-7 S. NandaGopalan, BIT
Chapter-7 S. NandaGopalan, BIT Example 3 - Car The Car class is modified by adding two more members and properties: private bool isDirty; private bool shouldRotate; public bool Dirty { get{ return isDirty; } set{ isDirty = value; } } public bool Rotate get{ return shouldRotate; } set{ shouldRotate = value; } Now, we shall declare a delegate as public delegate void CarDelegate( Car c); Chapter-7 S. NandaGopalan, BIT
Chapter-7 S. NandaGopalan, BIT Garage Class This delegate encapsulates a function pointer to some method taking a Car and returning void. public class Garage { // We have some cars. ArrayList theCars = new ArrayList(); public Garage() theCars.Add(new Car("Viper", 100, 0, true, false)); theCars.Add(new Car("Fred", 100, 0, false, false)); theCars.Add(new Car("BillyBob", 100, 0, false, true)); theCars.Add(new Car("Bart", 100, 0, true, true)); theCars.Add(new Car("Stan", 100, 0, false, true)); } Chapter-7 S. NandaGopalan, BIT
Chapter-7 S. NandaGopalan, BIT Garage Class… This method takes a CarDelegate as a parameter. Therefore, 'proc' is nothing more than a function pointer... public void ProcessCars(Car.CarDelegate proc) { ………. foreach(Car c in theCars) proc(c); } Chapter-7 S. NandaGopalan, BIT
Chapter-7 S. NandaGopalan, BIT CarApp Class public class ServiceDept { // A target for the delegate. public static void WashCar(Car c) if(c.Dirty) Console.WriteLine("Cleaning a car"); else Console.WriteLine("This car is already clean..."); } // Another target for the delgate. public static void RotateTires(Car c) if(c.Rotate) Console.WriteLine("Tires have been rotated"); Console.WriteLine("Don't need to be rotated..."); Chapter-7 S. NandaGopalan, BIT
Chapter-7 S. NandaGopalan, BIT Main() One way of using the delegates (because WashCar and RotateTires are static methods) Garage g = new Garage(); // Wash all dirty cars. g.ProcessCars(new Car.CarDelegate(WashCar)); // Rotate the tires. g.ProcessCars(new Car.CarDelegate(RotateTires)); If they are non-static, then use the following syntax: ServiceDept sd = new ServiceDept(); g.ProcessCars(new Car.CarDelegate(sd.WashCar)); g.ProcessCars(new Car.CarDelegate(sd.RotateTires)); Chapter-7 S. NandaGopalan, BIT
Chapter-7 S. NandaGopalan, BIT Multicasting Multicast delegate can call any number of Methods For multicasting you can use + operator which is overloaded Garage g = new Garage(); // Create two new delegates Car.CarDelegate w = new Car.CarDelegate (WashCar)); Car.CarDelegate r = new Car.CarDelegate (RotateTires)); g.ProcessCars(w + r); Chapter-7 S. NandaGopalan, BIT
Chapter-7 S. NandaGopalan, BIT Events An event is an action which you can respond to, or "handle," in code Events can be generated by a user action, such as clicking the mouse or pressing a key; by program code; or by the system (e.g. Mouse Click event) Events are widely used in GUI-based programming (VB6.0) Buttons, TextBox, ComboBox, etc report back to the enclosing Form (Listener). How is it useful in C# ? Take for example the Car class. When some thing goes abnormal (excess speed) an exception is raised and the program may halt! A better solution is to inform the object for some action. Delegates are classes commonly used within the .NET Framework to build event-handling mechanisms. Chapter-7 S. NandaGopalan, BIT
Chapter-7 S. NandaGopalan, BIT Establishing Events Events, however, need not be used only for graphical interfaces Events provide a generally useful way for objects to signal state changes that may be useful to clients of that object Events are an important building block for creating classes that can be reused in a large number of different programs Declaration of events is a two step process: define a delegate when an event occurs, the methods held by the delegate will be executed Define the events using C# 'event' keyword Chapter-7 S. NandaGopalan, BIT
Chapter-7 S. NandaGopalan, BIT Basic Event Model Chapter-7 S. NandaGopalan, BIT
Declaring an Event (Car Class) Hooking up / Subscribing to an Event Abstract Model Declaring an Event (Car Class) public delegate void EngineHandler(string msg); public static event EngineHandler Exploded; public static event EngineHandler AboutToBlow; Invoking / Firing an Event if(Exploded != null) Exploded("Sorry, this car is dead..."); Hooking up / Subscribing to an Event Car.Exploded += new Car.EngineHandler(sink.OnBlowUp); Car.AboutToBlow += new Car.EngineHandler(sink.OnAboutToBlow); Chapter-7 S. NandaGopalan, BIT
Car Example – Declaring an Event public class Car { // The delegate for our events. public delegate void EngineHandler(string msg); // This car can send these events. public static event EngineHandler Exploded; public static event EngineHandler AboutToBlow; ……… } Chapter-7 S. NandaGopalan, BIT
Chapter-7 S. NandaGopalan, BIT public void SpeedUp(int delta) { // If the car is dead, send event. if(carIsDead) if(Exploded != null) Exploded("Sorry, this car is dead..."); } else currSpeed += delta; // Almost dead? if(10 == maxSpeed - currSpeed) if(AboutToBlow != null) AboutToBlow("Careful, approaching terminal speed!"); // Still OK! if(currSpeed >= maxSpeed) carIsDead = true; Console.WriteLine("--> CurrSpeed = {0}", currSpeed); Chapter-7 S. NandaGopalan, BIT
Listening to Car Events public class CarEventSink { // OnBlowUp event sink A. public void OnBlowUp(string s) Console.WriteLine("Message from car: {0}", s); } public void OnAboutToBlow(string s) Chapter-7 S. NandaGopalan, BIT
Chapter-7 S. NandaGopalan, BIT Main() public class CarApp { public static int Main(string[] args) // Make a car as usual. Car c1 = new Car("SlugBug", 100, 10); // Make sink object. CarEventSink sink = new CarEventSink(); // Hook into events. Console.WriteLine("***** Hooking into car events *****"); Car.Exploded += new Car.EngineHandler(sink.OnBlowUp); Car.AboutToBlow += new Car.EngineHandler(sink.OnAboutToBlow); // Detach from events. Car.Exploded -= new Car.EngineHandler(sink.OnBlowUp); } Chapter-7 S. NandaGopalan, BIT
Chapter-7 S. NandaGopalan, BIT Bank Example An event will be raised when an overdraft occurs (you may use a callback delegate also) Account – Class OverDraftEventHandler – Delegate OnOverdraftHandler – Event Clients can subscribe using the following methods: AddOnOverdraft RemoveOnOverdraft EventArgs - is a base class for event argument classes. In case if your event has to pass additional information to its subscribers, a subclass is needed Chapter-7 S. NandaGopalan, BIT
Chapter-7 S. NandaGopalan, BIT Class Account class Account { protected int balance; // sender - ref to the object that raised the event // e - which describes the event - contains event arguments public delegate void OverdraftEventHandler (object sender, OverdraftEventArgs e); public event OverdraftEventHandler OnOverdraftHandler; public Account(int bal) balance = bal; } Chapter-7 S. NandaGopalan, BIT
Chapter-7 S. NandaGopalan, BIT public int Balance { get { return balance; } } public void Deposit(int aDeposit) if (aDeposit < 0) throw new ArgumentOutOfRangeException(); balance = balance + aDeposit; public bool Withdrawl(int aDebit) if(aDebit < 0) throw new ArgumentOutOfRangeException(); if(aDebit < balance) balance = balance - aDebit; return true; OverdraftEventArgs args = new OverdraftEventArgs(balance, aDebit); OnOverdraft(args); return false; Chapter-7 S. NandaGopalan, BIT
Chapter-7 S. NandaGopalan, BIT public void AddOnOverdraft(OverdraftEventHandler handler) { OnOverdraftHandler += handler; } public void RemoveOnOverdraft (OverdraftEventHandler handler) OnOverdraftHandler -= handler; protected void OnOverdraft(OverdraftEventArgs e) if(OnOverdraftHandler != null) OnOverdraftHandler(this, e); // firing the event Chapter-7 S. NandaGopalan, BIT
Chapter-7 S. NandaGopalan, BIT Event public class OverdraftEventArgs : EventArgs { protected int balance; protected int withdrawl; public OverdraftEventArgs(int bal, int wd) balance = bal; withdrawl = wd; } public int Balance get { return balance; } public int Withdrawl get { return withdrawl; } Chapter-7 S. NandaGopalan, BIT
Chapter-7 S. NandaGopalan, BIT Main public class Client { public static void OnOverdraft(object sender, OverdraftEventArgs e) Console.WriteLine("An overdraft occurred"); Console.WriteLine("The account balance is = {0}",e.Balance); Console.WriteLine("The with drawl was = {0}",e.Withdrawl); } public static void Main(string[] args) Account myAccount = new Account(100); Account.OverdraftEventHandler h = null; h = new Account.OverdraftEventHandler(OnOverdraft); myAccount.AddOnOverdraft(h); Chapter-7 S. NandaGopalan, BIT
Chapter-7 S. NandaGopalan, BIT End of Chapter 7 Chapter-7 S. NandaGopalan, BIT