Events Ashima Wadhwa Events Events are user actions

  • Slides: 11
Download presentation
Events Ashima Wadhwa

Events Ashima Wadhwa

Events • Events are user actions such as key press, clicks, mouse movements, etc.

Events • Events are user actions such as key press, clicks, mouse movements, etc. , or some occurrence such as system generated notifications. Applications need to respond to events when they occur. For example, interrupts. Events are used for inter-process communication.

Events • An event in C# is a way for a class to provide

Events • An event in C# is a way for a class to provide notifications to clients of that class when some interesting thing happens to an object. The most familiar use for events is in graphical user interfaces; typically, the classes that represent controls in the interface have events that are notified when the user does something to the control (for example, click a button).

Publisher • Publisher is responsible for publishing messages of various types. • A publisher

Publisher • Publisher is responsible for publishing messages of various types. • A publisher is an object that contains the definition of the event and the delegate. The event-delegate association is also defined in this object. A publisher class object invokes the event and it is notified to other objects.

subscriber • Subscriber captures messages of the type it is interested in. • A

subscriber • Subscriber captures messages of the type it is interested in. • A subscriber is an object that accepts the event and provides an event handler. The delegate in the publisher class invokes the method (event handler) of the subscriber class.

publisher-subscriber model. • The events are declared and raised in a class and associated

publisher-subscriber model. • The events are declared and raised in a class and associated with the event handlers using delegates within the same class or some other class. The class containing the event is used to publish the event. This is called the publisher class. Some other class that accepts this event is called the subscriber class. Events use the publisher-subscriber model.

Declaring Events • To declare an event inside a class, first a delegate type

Declaring Events • To declare an event inside a class, first a delegate type for the event must be declared. For example, public delegate void Boiler. Log. Handler(string status); • Next, the event itself is declared, using the event keyword: • //Defining event based on the above delegate event Boiler. Log. Handler Boiler. Event. Log;

using System; delegate void Handler(); // Declare the delegate class Incrementer { public event

using System; delegate void Handler(); // Declare the delegate class Incrementer { public event Handler Counted. ADozen; //Create and publish an event public void Do. Count() { for (int i=1; i<= 100; i++) if (i %12==0 &&Counted. ADozen != null) Counted. ADozen(); //raise the event every 12 counts. } }

class Dozens { public int Dozens. Count { get ; private set ; }

class Dozens { public int Dozens. Count { get ; private set ; } public Dozens(Incrementer incrementer) { Dozens. Count=0; incrementer. Counted. ADozen += Increment. Dozens. Count; } void Increment. Dozens. Count() { Dozens. Count++; } }

class Program { static void Main() { Incrementer incrementer = new Incrementer(); Dozens dozens.

class Program { static void Main() { Incrementer incrementer = new Incrementer(); Dozens dozens. Counter =new Dozens(incrementer); incrementer. Do. Count(); Console. Write. Line("Number of dozens = {0} " , dozens. Counter. Dozens. Count); } }

Output

Output