Chapter 9 Delegates and Events. Copyright 2006 Thomas P. Skinner2 Delegates Delegates are central to...

18

Click here to load reader

description

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++.

Transcript of Chapter 9 Delegates and Events. Copyright 2006 Thomas P. Skinner2 Delegates Delegates are central to...

Page 1: 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.

Chapter 9

Delegates and Events

Page 2: 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.

Copyright 2006 Thomas P. Skinner 2

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.

Page 3: 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.

Copyright 2006 Thomas P. Skinner 3

Pointers to Functions

• The following example shows the way we would use a pointer to a function in C/C++.

Page 4: 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.

Copyright 2006 Thomas P. Skinner 4

Example#include <iostream>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;

}

Page 5: 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.

Copyright 2006 Thomas P. Skinner 5

Output

Page 6: 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.

Copyright 2006 Thomas P. Skinner 6

The Delegate EquivalentDelegateEx1using 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); }

Page 7: 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.

Copyright 2006 Thomas P. Skinner 7

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); } }}

Page 8: 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.

Copyright 2006 Thomas P. Skinner 8

Multicasting

• Multicasting allows more than one method to be called when we invoke a delegate.

Page 9: 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.

Copyright 2006 Thomas P. Skinner 9

ExamplemyDelegate md = new myDelegate(A);md += new myDelegate(B);md(2);

Page 10: 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.

Copyright 2006 Thomas P. Skinner 10

Removing a DelegateDelegate md = new myDelegate(A);myDelegate del2 = new myDelegate(B);md += del2;md(1);md -= del2; //removes the delegatemd(2);

Page 11: 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.

Copyright 2006 Thomas P. Skinner 11

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.

Page 12: 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.

Copyright 2006 Thomas P. Skinner 12

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.

Page 13: 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.

Copyright 2006 Thomas P. Skinner 13

Event Handler ExampleEventHanderl1using 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; }

Page 14: 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.

Copyright 2006 Thomas P. Skinner 14

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);

Page 15: 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.

Copyright 2006 Thomas P. Skinner 15

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); } }

Page 16: 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.

Copyright 2006 Thomas P. Skinner 16

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);

Page 17: 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.

Copyright 2006 Thomas P. Skinner 17

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); } }}

Page 18: 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.

Copyright 2006 Thomas P. Skinner 18

The Output