Module 11 - Events

  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Module 11 - Events as PDF for free.

More details

  • Words: 250
  • Pages: 10
Module 11: Events By SRIRAM . B

Overview 

Events 

What is an event?



How events work?



Defining an event



Handling an event

Events 

Events allow an object to notify other objects that a change has occurred. The other objects can register an interest in an event, and they will be notified when the event occurs.





How Events work? 

Publisher raises an event to alert all interested objects (subscribers)



Subscriber provides a method to be called when the event is raised

Defining an event event mydelegate myevent; (To be declared within the class)

Events 

Subscribing to an Event obj.myevent = new mydelegate(obj.my); obj.myevent += new mydelegate(obj.my1);



Notifying obj.myevent();



Passing parameters to events 

Derive a class from EventArgs



Have the necessary get methods



Create the delegate with 2 parameters one is the sender and the other is the eventargs

Handling an event 

To handle an event we need to subscribe to the event by providing an event handler function whose signature matches that of delegate specified for use with the event.

Example - Events using System; using System.Timers; namespace events { public class EventHandler2 {

static int counter = 0;

static string str = "STG INTERNATIONAL LIMITED"; public static void WriteChar(object o, ElapsedEventArgs e) { Console.WriteLine(str[counter++ % str.Length]); }

Example - Events static void Main(string[] arg) {

Timer tm = new Timer(100); tm.Elapsed += new ElapsedEventHandler(WriteChar); tm.Start();

}

}

}

Console.ReadLine();

Session Ends

Exercise

Relax

Related Documents

Module 11 - Events
June 2020 4
Module 11
May 2020 12
11 - Hanzo Hattori's Events
November 2019 2
11 Data Module
April 2020 9
Module 11 Illustrator
August 2019 31