. Net series: Events and delegation,. net series event Delegation

Source: Internet
Author: User

. Net series: Events and delegation,. net series event Delegation
In the. net world, we can't do without delegation and events. In fact, it's very easy to understand. We have summarized it into six steps:1) define the delegate public delegate void RevicedEventHandler (object sender, RevicedEventArgs e); RevicedEventArgs: Custom parameter source, inherited to EventArgs class. 2) define the event public event RevicedEventHandler RevicedEvent; 3) define the function of triggering the event public virtual void OnReviced (RevicedEventArgs e) {if (RevicedEvent! = Null) {RevicedEvent (this, e) ;}} 4) defines the event processing function public void DoSome (object sender, RevicedEventArgs e ){.......} 5) register the event handler function (associate the event handler function with the event) this. revicedEvent + = new RevicedEvent (DoSome); 6) call an event to call person p = new person (); p. onReviced (new RevicedEventArgs ());Note: Steps 1st, 2, and 3 are defined in the publisher class, and steps 4 and 5 are defined in the subscription class. In fact,. net subsequently provides generic version delegation: public delegate void EventHandler <TEventArgs> (object sender, TEventArgs e );Therefore, the event in step 1 can be changed:Public event EventHandler <RevicedEventArgs> RevicedEvent; in this case, step 1 can be omitted.  The following is a practical example.For example, I need to rent a small room, but I don't have time to find it myself. So I went to the intermediary to ask for help, and I was notified after finding the room I needed (small apartment.Role: Subscriber (Subscriber) intermediary (Publisher of publisher)Business logic: the intermediary publishes my small apartment requirements to the housing pool (listed), and I subscribe to the small room requirements that I entrust to the intermediary. After the intermediary finds that the housing meets my needs, let me know. 

 
1 /// <summary> 2 // Publisher (intermediary) 3 /// </summary> 4 public class Publisher 5 {6 7 public delegate void FindedSmallHouseEventHandler (object sender, houseEventArgs e ); // define the delegate for finding a small house 8 9 10 /// <summary> 11 // define the event for finding a small house 12 /// </summary> 13 public event FindedSmallHouseEventHandler FindedSmallHouseEvent; 14 15 /// <summary> 16 // define the function that triggers the event 17 /// </summary> 18 /// <param name = "e"> </param> 19 public virtual void O NFindedSmallHouse (HouseEventArgs e) 20 {21 if (null! = FindedSmallHouseEvent) 22 {23 if (e. houseType = HouseType. small) // if it is a Small apartment, the event will be triggered. I am a diaosi, haha 24 {25 FindedSmallHouseEvent (this, e); 26} 27 28} 29} 30}
 

 

 
1 // <summary> 2 // subscriber (myself) 3 /// </summary> 4 public class Subscriber 5 {6 /// <summary> 7 // define the event handler for finding a small house 8 /// </summary> 9 // <param name = "sender"> </param> 10 // <param name = "e"> </param> 11 public void RevicedSmallHouse (object sender, houseEventArgs e) 12 {13 Console. writeLine ("I have a house, and I don't have to go to the streets any more. "); 14 Console. ReadKey (); 15} 16}
 

 

1 /// <summary> 2 // event parameter 3 /// </summary> 4 public class HouseEventArgs: EventArgs 5 {6 public HouseEventArgs (HouseType H) 7 {8 HouseType = H; 9} 10 11 12 public HouseType {get; set ;} 13} 14 15 // <summary> 16 // room type 17 // </summary> 18 public enum HouseType19 {20 // <summary> 21 // small apartment (diaosi) 22 /// </summary> 23 Small = /// <summary> 25 // General apartment (middle class) 26 /// </summary> 27 Normal, 28 /// <summary> 29 // large apartment (local tyrants) 30 /// </summary> 31 Big32}

 

 

 
1 class Program 2 {3 // <summary> 4 // call event 5 according to the specified conditions /// </summary> 6 // <param name =" args "> </param> 7 static void Main (string [] args) 8 {9 Publisher pub = new Publisher (); 10 Subscriber sb = new Subscriber (); 11 pub. findedSmallHouseEvent + = sb. revicedSmallHouse; // registers the event handler function 12 13 int I = int. minValue; 14 bool f = true; 15 while (f) 16 {17 Console. writeLine ("enter the room type you are looking! "); 18 19 if (int. tryParse (Console. readLine (). toString (), out I) 20 {21 if (0 <= I & I <= 2) // defines three types of room types (small, General, and large) 22 {23 HouseEventArgs eg = new HouseEventArgs (HouseType) I); 24 pub. onFindedSmallHouse (eg); 25} 26 27 28} 29 30 31} 32 33} 34}
 

 

 

 

 

Running result:

 

 

 

The example is complete. You are welcome to make a brick if it is not well written !!!

 

Related Article

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.