EVENT IN C#
An event is a mechanism via which a class can notify its clients when something happens.
Here i will explain c# events with a very simple yet an effective example, i hope after reading this post you will have a clear idea on how events work in C#.
First of all what is an EVENT?
Well, in simple terms, its a moment when something happens. this is true even in a real life scenario and not only in programming. when some thing happens how are you notified or how will you be acknowledged about it? This happens with an event handler. where the event is handled processed may be, and may also trigger more events.
Lets start with the code, shall we?
To begin with, lets have a idea as to what do we do here.
1 We create a class of type EventArgs.
2 Then we declare a delegate , this will have the event declaration.
3 We create the event.
4 We Handle this event.
5 We Fire(call) this event with the example code.
STEP 1 :We create a class of type EventArgs.
public class MyCustomEventArgs : EventArgs
{
public int MyValue ;
public MyCustomEventArgs(int input)
{
MyValue = input;
}
}
This is a custom class here we inherit the class EventArgs which becomes the base class for eventarguments in C#. If you need to pass arguments to the event handler, a specific EventArgs class has to be made, Eventually, a suitable EventArgs class might already be available, but in most cases, you will have to create one to tailor your specific arguments.
STEP 2 :Then we declare a delegate , this will have the event declaration.
public delegate void MyCustomEvent(object o, MyCustomEventArgs e);
Here this is created using delegates, for each subclass of EventArgs class u create this suitable delegate has to be created the best practice is to create this delegate inside the same scope.
STEP 3 :We declare the event and write the code how and when it is fired.
public event MyCustomEvent myEvent1;
The event is declared as of type it's delegate , the event is declared inside the class which has to implement the events.
For example:
public class MyEventDemo
{
public event MyCustomEvent myEvent1; //Event is declared here
/*
write other code part here
*/
}
STEP 4 :We Handle this event.
This involves two steps
1 registering the event
2 Handling the event
In the previous step we had declared the event inside the MyEventClass class. when we create an instance of this class in another class. we can also register the events involved with it. this is done with the overloaded += as shown below.
MyEventDemo myEventDemoObject = new MyEventDemo ();
myEventDemoObject.myEve1 +=new myEventHandler(myEventDemoObject_myEve1);
Also we can unregister it by using the overloaded -=
for Ex
myEventDemoObject.myEve1 -=new myEventHandler(myEventDemoObject_myEve1);
Next to handle the event
Note that we had given the method name object_myEve1 inside the deligate as an argument, this is the handler for the event. this handler method will take two parameters.
object sender and myEventArgs e
It is as shown below
void myEventDemoObject_myEve1(object sender, myEventArgs e) // This is the handler
{
Console.WriteLine("event 1 called and the value is :"+ e.MyValue.ToString()) ;
}
The complete code is given below
--------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace caEventsInCSharp
{
public delegate void MyCustomEvent(object o, MyCustomEventArgs e);
class Program
{
static void Main(string[] args)
{
MyEventHandlerClass rec = new MyEventHandlerClass();
rec.CallEvent();
Console.ReadKey();
}
}
public class MyEventHandlerClass
{
MyEventClass obj = new MyEventClass ();
public void CallEvent()
{
obj.myEvent1 +=new MyCustomEvent( obj_myEve1); // Handler is assigned
Console.WriteLine("Before event");
obj.DoSomething ();
Console.WriteLine("After event");
}
void obj_myEve1(object sender, MyCustomEventArgs e) // This is the handler
{
Console.WriteLine("event 1 called and the value is :"+ e.MyValue.ToString()) ;
}
}
public class MyEventClass
{
public event MyCustomEvent myEvent1; //Event is declared here
public void DoSomething()
{
if (myEvent1 != null)
{
int i = 0;
for (; i < 1000000000; i++) // Make the execution pause for a while
;
MyCustomEventArgs e = new MyCustomEventArgs(i);
myEvent1(null, e); // call the event
}
}
}
public class MyCustomEventArgs : EventArgs
{
public int MyValue;
public MyCustomEventArgs(int input)
{
MyValue = input;
}
}
}
--------------------------------------------------------------------------------------
Output
No comments:
Post a Comment