Events
When we do a mouse click, press keyboard, move mouse, an event is generated .Other than mouse and keyboard, touch screen, joystick also generate events . Events are a kind of notification which system have to respond. When this kind of notification is generated, system need to perform some work as defined.
For example, you are clicking on browser icon , 'click' generate some notification to the System, System find corresponding action with this notification, here System find that browser need to be open with this notification .System open the browser.
event handler is an object in a class, which raised an event with the help of the deligate . MSDN says that, there are two types of class. The class which is raising event, called publishers class. The class which is consuming the event is called subscriber class.It may also happened that a publisher class also consume events.
Publishers Class : A class which contain delegates and even definition. A publisher class as a mapping of event and delegates, a publisher class also accountant mapping other object who is need to invoke. This object may be in the publisher class or outside the publisher class.
Subscriber Class : publisher class invokes object of a subscriber class.
Here is the syntax of event declaration . You should check event is Null before using any event. an event is associated with an event Handler. An event and any event handler is declared in the same class.
Publisher class raise the event, subscriber class consume event. It may also occur that a event has no subscriber or event has multiple subscriber. When an event has multiple subscriber, method is called one by one synchronously.
An event can be defined with any of the access modifier.
The keyword can be applied on event is
When we declare an event in a class, it is associated with a delegate . Delegates and events both need to be declared in the class. Here is the example of event declaration. School class contain a delicate declaration, named it StudentDelicate. An event is declared named StudentEvent is associated with the StudentDelicate .
When we do a mouse click, press keyboard, move mouse, an event is generated .Other than mouse and keyboard, touch screen, joystick also generate events . Events are a kind of notification which system have to respond. When this kind of notification is generated, system need to perform some work as defined.
For example, you are clicking on browser icon , 'click' generate some notification to the System, System find corresponding action with this notification, here System find that browser need to be open with this notification .System open the browser.
event handler is an object in a class, which raised an event with the help of the deligate . MSDN says that, there are two types of class. The class which is raising event, called publishers class. The class which is consuming the event is called subscriber class.It may also happened that a publisher class also consume events.
Publishers Class : A class which contain delegates and even definition. A publisher class as a mapping of event and delegates, a publisher class also accountant mapping other object who is need to invoke. This object may be in the publisher class or outside the publisher class.
Subscriber Class : publisher class invokes object of a subscriber class.
Here is the syntax of event declaration . You should check event is Null before using any event. an event is associated with an event Handler. An event and any event handler is declared in the same class.
Publisher class raise the event, subscriber class consume event. It may also occur that a event has no subscriber or event has multiple subscriber. When an event has multiple subscriber, method is called one by one synchronously.
An event can be defined with any of the access modifier.
- Public
- Protected internal
- internal
- Private protected.
The keyword can be applied on event is
- Static : static method are allowed to call anytime.
- Virtual: events allow inheritance, derived class property of base class.An event can be override
- Sealed : an event can be make non inheritable to the derived class.
- Abstract : an event can we make abstract, the derived class can have the same event.
When we declare an event in a class, it is associated with a delegate . Delegates and events both need to be declared in the class. Here is the example of event declaration. School class contain a delicate declaration, named it StudentDelicate. An event is declared named StudentEvent is associated with the StudentDelicate .
class
School
{
public
delegate
void
StudentDelegate();
public
event
StudentDelegate
StudentEvent;
}
Example 1
Here is the example of a publisher and subscriber class. Delegates and
events are declared . The main method raise event GetFees is called.
//Subscriber
Class
class
Subscriber
{
static
void
Main(string[]
args)
{
Publisher
obj = new
Publisher();
obj.StudentEvent
+= new
Publisher.StudentDelegate(Example1);
obj.GetFees();
}
static
void
Example1()
{
Console.WriteLine("Execute
when event occure ");
}
}
//Publisher
Class
class
Publisher
{
public
delegate
void
StudentDelegate();
public
event
StudentDelegate
StudentEvent;
public
void
GetFees()
{
Console.WriteLine("Executed
when Bind event and delegate");
}
}
Example 2
class
Publisher
{
public
static
event
StudentDelegate
StudentEvent;
static
void
Main(string[]
args)
{
Subscriber
obj = new
Subscriber("Hellow
World");
StudentEvent
+= new
StudentDelegate(obj.SubscriberEvent1);
Process1();
}
public
static
void
Process1()
{
System.Console.WriteLine("Process1
fired");
}
}
public
delegate
void
StudentDelegate();
class
Subscriber
{
string
name;
public
Subscriber(string
nameArg)
{
name
= nameArg;
}
public
void
SubscriberEvent1()
{
System.Console.WriteLine("SubscriberEvent1
for object",
name);
}
}
No comments:
Post a Comment