I am a popular view of the incident in C #.

xiaoxiao2021-03-06  45

This is my popularity of the incident in C #, is more suitable for beginners (in fact, I am also an initiator) understanding of C # events, I found a lot of books because I just started, I found C #'s event principle, I found a lot of books. Understand, not suitable for beginners, so I want to talk about my views on it, may not be very accurate, if you are right, please encourage you, don't go, or don't standardize, please help correct Let's thank you.

http://blog.9cbs.net/curllion

Before studying C #, I have seen "Java Programming Thought", and some Java entry-level books, which explain the event monitoring, event adapters,

So I have always thought that in C #, RuntIming will always monitor the status of each control. If there is a change in the status of the control, the event will trigger the event.

Later, after reading some examples, I found that this idea was really stupid.

In fact, understanding the principle of incident C #, we must understand the following:

The change in the control property is changed in its class, which is changed by SET, which can be understood as a method of change, not a simple assignment. For example, Class Shape {Private Int Cvalue; Public Shape () {CVALUE = 0;} PUBLIC INT Colorue {Set {CVALUE = VALUE;} Get {Return CVALUE;}}}

Class mainapp {static void main (string [] argvs) {Shape Trigon = new shape (); trigon.colorvalue = 3;}}

Suppose, when Trigon's property ColorValue changes, a ColorChange event will be stimulated. If we need to send a message to the user when the ColorValue changes, for example: MessageBox.show ("The Color is Changed!"), Then, usually The practice is to define an event to Shape (how to define it here) ColorValueChange, first assume that it is defined, so: Class mainapp {static void main (string "argvs) {Shape Trigon = New Shape (); Trigon.colorchange = EventHandler (this.Shape_ColorValue); trigon.colorvalue = 3;} private void Shape_ColorValueChange (object sender, System.EventArgs e) {MessageBox.Show ( "the Color is Changed!");}} Thus, when the trigon ColorValue When it is changed, the event will be triggered; the above situation, and the following code, the result is equivalent: Class Shape {Private Int Cvalue; public shape () {CVALUE = 0;} public int colorValue {set {CVALUE = Value; MessageBox.show ("THE Color is Changd!");} Get {Return CVALUE;}}} Class Mainapp {static void main (string [] argvs) {Shape Trigon = New Shape (); Trigon.ColorValue = 3;}}, but, we define a class like this. If the user of the Shape class is not only to give the user a message, he still wants to achieve other purposes, this method will not pass, so C # has an event commissioned This stuff.

I understand this understanding of the event: Entrusted can accept a reference to the parameters and then perform this method.

So, we just need to put MessageBox.show ("The Color is ChangD!"); , Then make this method to the Shape class by delegate, let this instance execute this method.

Although this understanding is not very standardized, it is very helpful to understand the incident of C #, I hope that the master should not joke.

Let's take a complete implementation of the Shape class (now very well understood): using system; namespace winApp {// Defines an event entrustment type, notice that the event public delegate void colorChangeEvent (Object sender); public class shape {protected int CVALUE;

/ / Construct an event in the event of the event, PUBLIC Event ColorchangeEvent ColorChange; public int colorValue {set {CVALUE = value; colorChange (this); // triggered the event after the value of ColorValue changes. Get {return cvalue;}} public shape () {CVALUE = 0;

}

Here is an example of using the Shape class:..................

/ * Through a delegate, the reference trigon_ColorChange passed to the method of example shape, once the instance is executed ColorChange, trigon_ColorChange method will be called * / this.trigon.ColorChange = new WinApp.ColorChangeEvent (this.trigon_ColorChange);.. PRIVATE VOID Button1_Click (Object Sender, System.EventArgs E) {this.trigon.colorvalue = 3;}

Private Void Trigon_colorchange (Object Sender) {MessageBox.show ("The Color is Changed!");}.

[Finish]

转载请注明原文地址:https://www.9cbs.com/read-79645.html

New Post(0)