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.

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 16 Exception Handling. What is Exception Handling? A method of handling errors that informs the user of the problem and prevents the program from.
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Derived data types Dealing with data –Where the information is stored –What value is kept there –What kind of information is stored Address operator Pointers.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 5 Functions for All Subtasks.
Copyright © 2012 Pearson Education, Inc. Chapter 9 Delegates and Events.
Constructors And Instantiation. Constructor Basics Every class must have a constructor Even abstract classes!! No return types Their names must exactly.
CS  C++ Function Pointers  C# Method References  Groundwork for Lambda Functions.
JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.
תכנות ב C#. דוגמא לפלט using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program { static void.
Programming Based on Events
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
Delegates and Events Tom Roeder CS fa. Motivation – Function Pointers Treat functions as first-class objects eg. Scheme: (map myfunc somelist)
C# Event Processing Model Solving The Mystery. Agenda Introduction C# Event Processing Macro View Required Components Role of Each Component How To Create.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
Object Oriented Programming, Interfaces, Callbacks Delegates and Events Dr. Mike Spann
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Chapter 13 Working with Threads. Copyright 2006 Thomas P. Skinner2 Other Reference The book Programming Microsoft.NET by Jeff Prosise. Chapter 14 is excellent.
COMP 205 – Week 10 Chunbo Chu. Method Local variables The existence of a local variable is limited to the block in which it is created and the blocks.
CSC 298 Windows Forms.
Copyright © 2006 Thomas P. Skinner1 Chapter 5 Indexers, Interfaces, and Enumerators.
CIS 3301 C# Lesson 13 Interfaces. CIS 3302 Objectives Understand the Purpose of Interfaces. Define an Interface. Use an Interface. Implement Interface.
Features of Object Oriented Programming:  A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined.
Multiple Files. Monolithic vs Modular  one file before  system includes  main driver function  prototypes  function.
1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
Module 8: Delegates and Events. Overview Delegates Multicast Delegates Events When to Use Delegates, Events, and Interfaces.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Java and C++ Transitioning. A simple example public class HelloWorldApp { public static void main(String[] args) { //Display the string. System.out.println("Hello.
Chapter -6 Polymorphism
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
1.NETDelegates & eventsNOEA / PQC Delegates & events Observer pattern Delegates –Semantics –Cil – kode –Anvendelse Events.
From C++ to C# Part 5. Enums Similar to C++ Similar to C++ Read up section 1.10 of Spec. Read up section 1.10 of Spec.
CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2.
Special Features of C# : Delegates, Events and Attributes.
Events Programming in C# Events CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
V 1.0 OE-NIK HP 1 Advanced Programming Delegates, Events.
1 Inheritance and Polymorphism Chapter Getting Started Continue the Cat Management example from previous presentation.
CPSC 252Inheritance II Page 1 Inheritance & Pointers Consider the following client code: const int MAXCLOCKS = 2; Clock* clockPtr[ MAXCLOCKS ]; clockPtr[0]
Object Oriented Programming Lecture 2: BallWorld.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
INF230 Basics in C# Programming
Delegates and Events Svetlin Nakov Telerik Corporation
התוכנית: using System; using System.Collections.Generic;
using System; namespace Demo01 { class Program
OBJECT ORIENTED PROGRAMMING
Creational Pattern: Prototype
Methods Attributes Method Modifiers ‘static’
Chapter 5 Classes.
Pointers and Pointer-Based Strings
Pointers Psst… over there.
Pointers Psst… over there.
C# Event Processing Model
Chapter 5 Function Basics
Pointers & Functions.
عرض اجمالي المهام الشرطية في سي شارب (الأمر if)
Delegates & Events 1.
Method Overloading in JAVA
class PrintOnetoTen { public static void main(String args[]) {
Programming in C# CHAPTER - 8
Pointers and Pointer-Based Strings
foo.h #ifndef _FOO #define _FOO template <class T> class foo{
Pointers & Functions.
Object Oriented Programming
Events, Delegates, and Lambdas
Presentation transcript:

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

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

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

Copyright 2006 Thomas P. Skinner5 Output

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

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

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

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

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

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.

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.

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

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

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

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

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

Copyright 2006 Thomas P. Skinner18 The Output