programming usage of add and remove accessors and how to selectively call methods of the event subscriber class.
Using system; using system. collections. generic; using system. LINQ; using system. text; namespace advancedevent {// define a delegate void namechangeddelegate (string name, string newvalue); Class program {static void main (string [] ARGs) {customer C = new customer (); subscriber S1 = new subscriber (C, "subscribe-a"); subscriber S2 = new subscriber (C, "subscribe-B "); subscriber S3 = new subscriber (C, "subscribe-c"); C. firstname = "Kevin"; C. lastname = "Hoffman"; s2.unsubscribe (); C. firstname = "Joe"; C. middlename = "Scott"; console. readline () ;}} class subscriber // {private string subscribedid = "new subscriber"; private customer mycustomer = NULL; private namechangeddelegate ncdel = NULL; public subscriber (customer C, string subid) {subscribedid = subid; ncdel = new namechangeddelegat E (mycustomer_onnamechanged); mycustomer = C; mycustomer. onnamechanged + = ncdel;} void mycustomer_onnamechanged (string name, string newvalue) {console. writeline ("[{0}] Customer {1} changed to {2 }. ", subscribedid, name, newvalue);} public void unsubscribe () {mycustomer. onnamechanged-= ncdel;} Public String subscriberid {get {return subscribedid;} set {subscribedid = value ;}} class cust Omer {private string firstname; private string lastname; private string middlename; Private event namechangeddelegate onnamechange; // create the event by using the delegate defined public event namechangeddelegate onnamechanged // define the details of the event {Add {onnamechange + = value; If (value. target is subscriber) {console. writeline ("subscriber '{0}' Just subscribed to onnamechanged. ", (Subscriber) value. target ). subscriberid) ;}}remove {onnamechange-= value; If (value. target is subscriber) {console. writeline ("subscriber '{0}' Just subscribed to onnamechanged. ", (subscriber) value. target ). subscriberid) ;}}public string firstname {get {return firstname ;}set {firstname = value; onnamechange ("firstname", value );}} public String lastname {get {return lastname;} Set {lastname = value; onnamechange ("lastname", value) ;}} Public String middlename {get {return middlename;} set {middlename = value; policyallbut ("subscribe-c", "middlename", value) ;}} private void policyallbut (string subid, string name, string value) {foreach (namechangeddelegate D in onnamechange. getinvocationlist () {If (subscriber) d. target ). subscriberid. toupper ()! = Subid. toupper () {d (name, value );}}}}}
Classes or objects can notify other classes or objects of related events through events. The class for sending (or triggering) events is called "publisher", and the class for receiving (or processing) events is called "subscription ".
In this example, the customer is the publisher, and the user who receives and processes the event is the subscriber. The entire event processing process is designed as follows:
- Define a delegate namechangeddelegate to bind to the subscriber event processing method mymermer_onnamechanged.
Delegate void namechangeddelegate (string name, string newvalue); // declares a delegate for the event handling method
Class subscriber
{
Private namechangeddelegate ncdel = NULL; // declare the delegate as a member of the class
Public subscrber (customer C, string subid)
{
Ncdel = new namechangeddelegate (mycustomer_onnamechanged); // bind the delegate to the Event Processing Method
Mycustomer = C;
Mycustomer. onnamechanged + = ncdel; // customizes the processing method for the onnamechanged method of the customer class. If an event occurs, the mycustomer_onnamechanged event is triggered to run.
}
}
- Write the specific event handling method in the subscriber class.
Void mycustomer_onnamechanged (string name, string newvalue)
{
// Processing Method
}
- The above steps are explained, and the handling method after the event occurs. However, the event has not been written yet. Now write the key part and write the event.
- Two events are defined in the customer class.
Private event namechangeddelegate onnamechange;
Public event namechangeddelegate onnamechanged
{
Add // Add accessors
{
}
Remove // remove accessors
{
}
}
- First, let's take a look at the role of onnamechange.
Public String firstname
{
Get
{
Return firstname;
}
Set
{
Firstname = value;
Onnamechange ("firstname", value );
}
}
Onnamechange is an instance of namechangeddelegate. Therefore, the onnamechange ("firstname", value) and namechangeddelegate delegate Methods mymermer_onnamechanged have the same parameter structure.
The onnamechange ("firstname", value) Statement in the firstname attribute above is run only when the firstname value of the customer instance changes. Run the mycustomer_onnamechanged method at the same time. The onnamechange ("firstname", value) statement runs only when the firstname value of the customer instance changes. I think it only has something to do with this sentence: onnamechange + = Value
- Analyze the onnamechanged event.
Public event namechangeddelegate onnamechanged
{
Add
{
Onnamechange + = value;
If (value. Target is subscriber)
{
Console. writeline ("subscriber '{0}' Just subscribed to onnamechanged.", (subscriber) value. Target). subscriberid ));
}
}
Remove
{
Onnamechange-= value;
If (value. Target is subscriber)
{
Console. writeline ("subscriber '{0}' Just subscribed to onnamechanged.", (subscriber) value. Target). subscriberid ));
}
}
}