Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 9 Delegates and Events. Copyright 2006 Thomas P. Skinner2 Delegates Delegates are central to the.NET FCL A delegate is very similar to a pointer.

Similar presentations


Presentation on theme: "Chapter 9 Delegates and Events. Copyright 2006 Thomas P. Skinner2 Delegates Delegates are central to the.NET FCL A delegate is very similar to a pointer."— Presentation transcript:

1 Chapter 9 Delegates and Events

2 Copyright 2006 Thomas P. Skinner2 Delegates Delegates are central to the.NET FCL A delegate is very similar to a pointer to a function in C or C++. Since pointers are undesirable in C# programming the delegate is the proper alternative. A delegate is a prototype for a method. We instantiate a delegate and set it to reference one or more methods to be called when we invoke the delegate.

3 Copyright 2006 Thomas P. Skinner3 Pointers to Functions The following example shows the way we would use a pointer to a function in C/C++.

4 Copyright 2006 Thomas P. Skinner4 Example #include using namespace std; void A(int i) { cout << "A called with " << i << endl; } void B(int i) { cout << "B called with " << i << endl; } void main() { void (*pf)(int i); pf=A; pf(1); pf=B; pf(2); return; }

5 Copyright 2006 Thomas P. Skinner5 Output

6 Copyright 2006 Thomas P. Skinner6 The Delegate Equivalent DelegateEx1 using System; using System.Collections.Generic; using System.Text; namespace DelegateEx1 { delegate void myDelegate(int i); class Program { static void A(int i) { Console.WriteLine("A called with {0}", i); }

7 Copyright 2006 Thomas P. Skinner7 The Delegate Equivalent static void B(int i) { Console.WriteLine("B called with {0}", i); } static void Main(string[] args) { myDelegate md = new myDelegate(A); md(1); md = new myDelegate(B); md(2); }

8 Copyright 2006 Thomas P. Skinner8 Multicasting Multicasting allows more than one method to be called when we invoke a delegate.

9 Copyright 2006 Thomas P. Skinner9 Example myDelegate md = new myDelegate(A); md += new myDelegate(B); md(2);

10 Copyright 2006 Thomas P. Skinner10 Removing a Delegate Delegate md = new myDelegate(A); myDelegate del2 = new myDelegate(B); md += del2; md(1); md -= del2; //removes the delegate md(2);

11 Copyright 2006 Thomas P. Skinner11 Events Events are really just special instances of delegates. By convention they have a specific declaration for the delegate: delegate void MyEvent(object sender, EventArgs e); We declare an event like this: event MyEvent me; We use an event just like a delegate except that only the += and -= operators can be used by classes other than the one declaring the event.

12 Copyright 2006 Thomas P. Skinner12 Event Example The following example demonstrates creating your own event handler and class derived from EventArgs. It also shows that any class other than the one declaring the event can only use the += and -= operators on the event.

13 Copyright 2006 Thomas P. Skinner13 Event Handler Example EventHanderl1 using System; using System.Collections.Generic; using System.Text; using System.IO; namespace EventHandler1 { public delegate void myEvent(object sender, MyEventArgs e); public class MyEventArgs : EventArgs { public int i; }

14 Copyright 2006 Thomas P. Skinner14 Event Handler Example class Program { public event myEvent me; static void Main(string[] args) { (new Program()).Run(); } void Run() { MyEventArgs e = new MyEventArgs(); e.i = 1; me = new myEvent(A); Trigger(this, e);

15 Copyright 2006 Thomas P. Skinner15 Event Handler Example (new tryEvent(this)).addEvent(); } public void Trigger(object sender, MyEventArgs e) { me(sender, e); //triger the event } void A(object sender, MyEventArgs e) { Console.WriteLine("A called with {0}", e.i); }

16 Copyright 2006 Thomas P. Skinner16 Event Handler Example class tryEvent { Program p; public tryEvent(Program p) { this.p = p; } public void addEvent() { MyEventArgs e = new MyEventArgs(); e.i = 2; p.me += new myEvent(B);

17 Copyright 2006 Thomas P. Skinner17 Event Handler Example //the next statement would fail if compiled //p.me = new myEvent(B); p.Trigger(this, e); //trigger the event } void B(object sender, MyEventArgs e) { Console.WriteLine("B called with {0}", e.i); }

18 Copyright 2006 Thomas P. Skinner18 The Output


Download ppt "Chapter 9 Delegates and Events. Copyright 2006 Thomas P. Skinner2 Delegates Delegates are central to the.NET FCL A delegate is very similar to a pointer."

Similar presentations


Ads by Google