Understanding and understanding of events

Source: Internet
Author: User

Understanding and understanding of events

A class or object can notify other classes or objects of related events through events.

The publisher determines when an event is triggered, and the user determines the operation to be performed to respond to the event.

In C #, event processing is actually a kind of delegate with special signatures.

Assume that the student receives the score immediately after the instructor registers the score.

The code that the publisher must first write

1. Define a delegate and event and write a method. When this method is called, The Event Notification subscriber is triggered.

Public class Teacher {public delegate void TellScoreEventHandler (object sender, ScoreEventArgs e); public event TellScoreEventHandler tellScoreEvent; /// <summary> // notify the subscriber of an event // </summary> /// <param name = "e"> </param> public void OnTellScore (ScoreEventArgs e) {if (tellScoreEvent! = Null) tellScoreEvent (this, e) ;}/// <summary> // when calling a method, trigger event /// </summary> /// <param name = "name"> </param> /// <param name = "score"> </param> public void tellstudentsocret (string name, int score) {ScoreEventArgs scoreArgs = new ScoreEventArgs (name, score); OnTellScore (scoreArgs );}} /// <summary> /// class of Custom Event Data /// </summary> public class ScoreEventArgs: EventArgs {public int Score {get; set ;} public string Name {get; set;} public ScoreEventArgs (string name, int score) {this. name = name; this. score = score ;}}

2. Add a subscriber first, and then call the method in which the event can be triggered by the publisher.

Student stu = new Student (); Teacher teacher = new Teacher (); teacher. tellScoreEvent + = new Teacher. tellScoreEventHandler (stu. teacher_tellScoreEvent); teacher. tellstudentsocres ("QB struggling", 100 );

3. How to handle an event in a subscriber?

Public class Student {// how to handle the public void teacher_tellScoreEvent (object sender, ScoreEventArgs e) {MessageBox. show (e. name + "tested" + e. score. toString ());}}

 

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.