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:
Public class Program {// The FIELD private int myValue to be monitored; // attribute setting. The event trigger function public bool MyValue {get {return myValue;} is called here ;} set {// call the event-triggered function if (value! = MyValue) {WhenMyValueChange () ;}myvalue = value ;}// defined delegate public delegate void MyValueChanged (object sender, EventArgs e ); // The public event MyValueChanged OnMyValueChanged associated with the delegate event; // initiate the initial value of the constructor and bind it to an event handler public Program () {myValue = 0; onMyValueChanged + = new MyValueChanged (afterMyValueChanged);} // event processing function, add the private void afterMyValueChanged (object sender, EventArgs e) operation here {// do some Thing} // event-triggered function private void WhenMyValueChange () {if (OnMyValueChanged! = Null) {OnMyValueChanged (this, null );}}}