Events - Object-Oriented Programming


Using delegates in classes, you can describe events. Events are a way of interacting between objects of different classes. Objects of the same class can use objects of other classes. To do this, they create objects of the required class and call its methods (pass messages to it). When working with the called methods, it often happens that you need to send messages about the emerged situations (important changes in your work) to the objects of the calling class, without completing their work. To transfer such messages, special elements of the class-events are used.

The class that is required to report the occurrence of certain event situations, should be able to do the following:

• declare the type of the delegate corresponding to the event;

• declare event in the class (delegate instance);

• Initiate the event at the appropriate moment, passing the parameters necessary for its operation to the handler (that is, in some way, notify the class clients that it has an event).

By triggering an event , the class sends a message to the recipients of the event - objects of some other classes. The class that initiates the event is called the message sender . Classes whose objects receive messages are called classes - recipients message . The sender class of the message does not know its recipients in principle. It simply initiates the event. The same message can be received and processed differently by an arbitrary number of objects of different classes.

The interfaces and many classes from the FCL library have a standard set of predefined events.

Announcing an event in a class

The message sent when the event is triggered is an analog of the method call. Because the signature of the message sent must match the signature of the received message, the declaration of the event syntactically must specify the method signature.

In C #, events are implemented using delegates. Each event is specified by a delegate describing the signature of the method that will handle the messages (the event handler). The announcement of an event is a two-stage process:

• The delegate is first declared, as previously discussed. The delegate declaration is described in a certain class. But often this ad is outside the class in the namespace. For example:

public delegate & lt; type & gt; & lt; delegate_name & gt; ([parameters]);

• If the delegate is defined, then in the class that creates the events, it's enough to declare the event as an instance of the corresponding delegate. The event keyword is used (this ensures that the instance of the event can not be called in other classes). The event is declared as follows:

public event & lt; delegate_name & gt; & lt; event_name & gt ;;

The following is an example of declaring an delegate and an event that represents an instance of this delegate:

// Delegate declaration - a new data type public delegate void MyHandler (object o, int n);

// Declaration of class event - delegate instance public event MyHandler MyEvent;

To initiate an event, you simply need to invoke an instance of the delegate. For example:

// check that event handlers are set if (MyEvent! = null) MyEvent (this, i); // start the event

Below is an example of a class description that contains a method that takes a long time to complete. This method reports the implementation of every tenth of its work. Objects using this method can visually inform users about the progress of work in this method:

// Declaring a new delegate type with parameters:

// o - reference to the object that triggered the event

// n - percentage of the work done

public delegate void MyHandler (object o, int n);

// declare a class with an event

public class ClassWithEvent {// class with event

// Create delegate instance - event

public event MyHandler MyEvent;

// class field private int Volume = 0;

// class constructor - parameter - the amount of work public EventClass (int p) {if (p & gt; 0) Volume = p;}

// description of the method that does a long job

// (but in this example, meaningless)

public void LongWork () {

double s = 0; int k = 0;

int st = Volume/10; // the tenth part of the work

for (int i = 0; i & lt; Volume; i ++) {

s + = Math.Atan (Math.Sqrt (i) * Math.Sqrt (i)); if (k == st) {// the specified part of the job is executed if (MyEvent! = null) {

int n = (int) (i * 100.0)/Volume;

MyEvent (this, n); // start the event


k = 0;


else k + = 1;




In this example, the ClassWithEvent class describes the MyHandler delegate, whose signature contains three parameters. The MyEvent event in ClassWithEvents is an instance of the class specified by the delegate.

Handling events in classes

A class that is interested in handling events must:

• have an event handler - a method matched by the signature with the signature of the delegate that sets the event;

• have a reference to the object that creates the event to access this event-an event object;

• Attach the event handler to the event object. This is implemented using the + = operation for the described event.

An example of a class that uses the ClassWithEvent class described above with an event is shown below:

class Program {

const int WorkVolume = 10000000; // the amount of work public static void Main () {

// an object is created

ClassWithEvent obj = new ClassWithEvent (WorkVolume); // set the event handler

obj.MyEvent + = new MyHandler (ShowStar);

// run a long job for the object and obj.LongWork ();

// wait for the Console key to be pressed. ReadLine ();


// event handler

private static void ShowStar (object o, int n) {

Console.WriteLine ( Done {0}% & quot ;, n);

// Console.Write ( * ); // you can just print *



Also We Can Offer!

Other services that we offer

If you don’t see the necessary subject, paper type, or topic in our list of available services and examples, don’t worry! We have a number of other academic disciplines to suit the needs of anyone who visits this website looking for help.

How to ...

We made your life easier with putting together a big number of articles and guidelines on how to plan and write different types of assignments (Essay, Research Paper, Dissertation etc)