About event-handling mechanisms in C #

Source: Internet
Author: User
Tags implement include
Today a friend asked me how to implement the event in the program. So I summed up the HA sent out. To understand the event, you first need to know what the delegate is. Delegate in C # to allow developers
Passes a method in one object to another object that can call the method's class. For example, a method in class AC can be add[if the method is included in a delegate and passed to another class WMS. At this time the class WMS can invoke the add in class AC. Of course, no matter how you implement the method of delivery, can be static, can also be instantiated. To implement a delegate, you need to follow this without beating:
A: Declare the delegate object. Note: The parameters must be the same as the parameters of the method you want to include.
public delegate void Neweggdelegate (String values)
B: Defining methods
Class neweggclass1{
Public Viod NeweggMethod1 (String values) {}
Public Viod NeweggMethod2 (String values) {}
....}
C: Create a delegate object to include this method in the delegate object at this time
Class neweggclass2{
Public Neweggdelegate createdelegate ()
{NeweggClass1 obj1=new NeweggClass1 ();
Neweggdelegate dele1=new neweggdelegate (obj1. NEWEGGMETHOD1);
Neweggdelegate dele2=new neweggdelegate (obj1. NEWEGGMETHOD2);
Neweggdelegate dele3=dele1+dele2;//belonged to the multi-point entrusted
return dele3;
}
}
D: Then call the method contained in the delegate.
Class NEWEGGCLASS3
{
public void Neweggcalldelegate (Neweggdelegate de,string values)
{de (values);}
}
Class Test
{
static void Main (string[] args)
{
NeweggClass2 cls2=new NeweggClass2 ();
Neweggdelegate delel=cls2.createdelegate ();
NEWEGGCLASS3 cls3=new NEWEGGCLASS3 ();
Cls3. Neweggcalldelegate (Delel, "Deleagte is used");
}
}
An event is a delegate object that has a specific parameter. Format like: public delegate void Neweggeventhandler (Object Sender,neweggeventargs e);
Sender represents the object that triggers the event, e means that some of the data that can be used in the event handler function can be inherited from the Evenetargs class Neweggeventargs. Of course, sometimes do not need to derive their own class to think of some GUI program, there are many county events using such as: MouseEventArgs. Otherwise, you must derive from the EventArgs class.
Here are some examples:
Using System;
Step 1: Declare the delegate object
public delegate void MyHandler1 (Object Sender,myeventargs e);
public delegate void MyHandler2 (Object Sender,myeventargs e);
Step 2: Create the event handler function method
Class a{
Public const string M_id= "Class A";
public void OnHandler1 (Object Sender,myeventargs e) {
Console.WriteLine ("I AM in OnHandler1 and MyEventArgs is {0}",
E.M_ID);
}
public void OnHandler2 (Object Sender,myeventargs e) {
Console.WriteLine ("I AM in OnHandler2 and MyEventArgs is {0}",
E.M_ID);
}
Step 3: Create the delegate object, and the event handler function contains the object in which the event will be triggered
Public A (b) {
MyHandler1 d1=new MyHandler1 (OnHandler1);
MyHandler2 d2=new MyHandler2 (OnHandler2);
B.event1 +=d1;
B.event2 +=d2;
}
}
Step 4: Invoke the included method through the delegate object, which is the triggering event
Class b{
public event MyHandler1 Event1;
public event MyHandler2 Event2;
public void FireEvent1 (MyEventArgs e) {
if (Event1!= null) {
Event1 (this,e);
}
}
public void FireEvent2 (MyEventArgs e) {
if (Event2!= null) {
Event2 (this,e);
}
}
}
public class MyEventArgs eventargs{
public string m_id;
}
public class driver{
public static void Main () {
b b= new B ();
A a= new A (b);
MyEventArgs e1=new MyEventArgs ();
MyEventArgs e2=new MyEventArgs ();
e1.m_id = "Event args for event 1";
e2.m_id = "Event args for event 2";
B.fireevent1 (E1);
B.fireevent2 (E2);
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.