C # Changing event monitoring variable values

Source: Internet
Author: User

How to Use the C # event to monitor variable changes? This is a very common problem. In addition, if events can be used to solve the problem, it will bring great convenience to programming while maintaining excellent performance.

The complete code is as follows:

01 public class Program 02 { 03      // Fields to be monitored 04      private int myValue; 05   06      // Attribute settings. The event-triggered function is called here. 07      public bool MyValue 08      { 09          get { return myValue; } 10          set 11          { 12              // Call the event-triggered function if the variable changes 13              if (value!=myValue) 14              { 15                  WhenMyValueChange(); 16              } 17              myValue = value;  18          } 19      } 20   21      // Define the delegate 22      public delegate void MyValueChanged( object sender, EventArgs e); 23      // Events associated with Delegation 24      public event MyValueChanged OnMyValueChanged; 25   26      // Initiate the initial value of the constructor and bind an event handler. 27      public Program() 28     <

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.