Presentation is loading. Please wait.

Presentation is loading. Please wait.

DELEGATES AND EVENT MODELING

Similar presentations


Presentation on theme: "DELEGATES AND EVENT MODELING"— Presentation transcript:

1 DELEGATES AND EVENT MODELING
Created by : Asst. Prof. Ashish Shah DELEGATES AND EVENT MODELING

2 DELEGATES INTRODUCTION
Created by : Asst. Prof. Ashish Shah DELEGATES INTRODUCTION DELEGATES ALLOWS THE PROGRAMMER TO ENCAPSULATE A REF. TO A METHOD INSIDE A DELEGATE OBJECT. DELEGATE OBJECT CAN THEN BE PASSSED TO CODE WHICH CAN CALL THE REFERENCED METHOD, WITHOUT HAVING TO KNOW AT COMPILE TIME WHICH METHOD WILL BE INVOKED.

3 PROPERTIES OF DELEGATES
Created by : Asst. Prof. Ashish Shah PROPERTIES OF DELEGATES IT IS A TYPE SAFE. IT ALLOWS METHODS TO BE PASSED AS PARAMETERS. IT CAN BE USED TO DEFINE CALLBACK METHODS. IT CAN BE USED TO CHAIN TOGETHER ie. MULTIPLE METHODS CAN BE CALLED ON A SINGLE EVENT.

4 WHERE DELEGATES ARE USED?
Created by : Asst. Prof. Ashish Shah WHERE DELEGATES ARE USED? USED WHERE THE METHOD NEEDS TO BE INVOKED BY THE RUNTIME WHEN THE EVENT OCCURS, HENCE THIS METHOD IS PASSED AS A PARAMETER TO A DELEGATE. TO START A NEW THREAD FOR A METHOD WHICH IS SUPPOSED TO BE PASSED TO DELEGATE. IT IS ALSO USED AS GENERIC CLASS LIBRARIES WHICH HAVE GENERIC FUNCTIONALITY DEFINED. THIS CAN BE DONE BY PASSING THE USER DEFINED FUNCTIONS TO DELEGATES.

5 using System; public delegate void deleint(int x); public delegate void deleString(string x); class eg_of_dele { public void inc(int n) c.w.(“after increment : “ + ++n); } class eg_of_dele2 public void strshow(string s) c.w.(“hello : “ + s); class del_eg_main psvm(string []args) eg_of_dele d1=new eg_of_dele(); deleint dint=new deleint(d1.inc); dint(5); eg_of_dele d2=new eg_of_dele(); deleint dstr=new deleString(d2.strshow); dint(“jmpc”); Created by : Asst. Prof. Ashish Shah

6 Events Event can be visualized as follows.
Created by : Asst. Prof. Ashish Shah Event can be visualized as follows. Object notifies everyone that an event has occured An event occurs in an object Some one acts upon it Someone listens for that event

7 Event mechanism Declare a delegate Fire the events
Created by : Asst. Prof. Ashish Shah Following figure illustrates the event raising mechanism. Event models basic idea is “publisher and subscribers”. Declare a delegate Declare an event based on delegate Fire the events Application2 (subscriber) Application1 (subscriber) Subscriber event Subscriber event Fire the event handler Fire the event handler Event handler Event handler

8 Example of event handling using delegates
using System; public delegate void eventcheck(); class event_impl { public event eventcheck click; pubilc void onClick() if(click != null) click(); } class event_example public static void handle() c.w.(“click event occurs”); psvm(string [] args) event_impl ei=new event_impl(); ei.click += new eventcheck(handle); ei.onClick(); o/p: click event occurs Created by : Asst. Prof. Ashish Shah Example of event handling using delegates


Download ppt "DELEGATES AND EVENT MODELING"

Similar presentations


Ads by Google