Event Accessors
Mentioned earlier that the
To change the operation of these operators, you must define event accessors for the event.
When event accessors are declared, the event does not contain an embedded delegate object. You must implement your own storage mechanism for storing and
removing the methods registered with the event.
Lets change the previous
The code will display following output:
+=
and -=
operators are the only operators allowed for an event. It is possible, however, to change these
operators’ behavior and have the event perform whatever custom code you like when they are used.
To change the operation of these operators, you must define event accessors for the event.
- There are two accessors:
add
andremove
. - The declaration of an event with accessors looks similar to the declaration of a property.
public event [Delegate Type] [Event Handler Name]
{
add
{
... // Code to implement the =+ operator
}
remove
{
... // Code to implement the -= operator
}
}
Lets change the previous
incrementer
class:
class incrementer { public event myEventHandler _reachTen, _reachDozen, _reachFifty, _reachHundreds; public event myEventHandler reachTen { add { Console.WriteLine("Subscribing reachTen: " + value.ToString()); _reachTen += value; } remove { Console.WriteLine("Unsubscribing reachTen: " + value.ToString()); _reachTen -= value; } } public event myEventHandler reachDozen { add { Console.WriteLine("Subscribing reachDozen: " + value.ToString()); _reachDozen += value; } remove { Console.WriteLine("Unsubscribing reachDozen: " + value.ToString()); _reachDozen -= value; } } public event myEventHandler reachFifty { add { Console.WriteLine("Subscribing reachFifty: " + value.ToString()); _reachFifty += value; } remove { Console.WriteLine("Unsubscribing reachFifty: " + value.ToString()); _reachFifty -= value; } } public event myEventHandler reachHundreds { add { Console.WriteLine("Subscribing reachHundreds: " + value.ToString()); _reachHundreds += value; } remove { Console.WriteLine("Unsubscribing reachHundreds: " + value.ToString()); _reachHundreds -= value; } } public void startIncrement(int start, int end) { for (int counter = start; counter <= end; ++counter) { if ((counter % 10 == 0) && (_reachTen != null)) { Console.Write("current counter: " + counter + " "); _reachTen(); } if ((counter % 12 == 0) && (_reachDozen != null)) { Console.Write("current counter: " + counter +" "); _reachDozen(); } if ((counter % 50 == 0) && (_reachFifty != null)) { Console.Write("current counter: " + counter + " "); _reachFifty(); } if ((counter % 100 == 0) && (_reachHundreds != null)) { Console.Write("current counter: " + counter + " "); _reachHundreds(); } } } }
The code will display following output:
Subscribing reachDozen: events.myEventHandler
Subscribing reachFifty: events.myEventHandler
Subscribing reachHundreds: events.myEventHandler
current counter: 10 counting reach Ten
current counter: 12 counting reach Dozen
current counter: 20 counting reach Ten
current counter: 24 counting reach Dozen
current counter: 30 counting reach Ten
current counter: 36 counting reach Dozen
current counter: 40 counting reach Ten
current counter: 48 counting reach Dozen
current counter: 50 counting reach Ten
current counter: 50 counting reach Fifty
current counter: 60 counting reach Ten
current counter: 60 counting reach Dozen
current counter: 70 counting reach Ten
current counter: 72 counting reach Dozen
current counter: 80 counting reach Ten
current counter: 84 counting reach Dozen
current counter: 90 counting reach Ten
current counter: 96 counting reach Dozen
current counter: 100 counting reach Ten
current counter: 100 counting reach Fifty
current counter: 100 counting reach Hundreds
Subscribing reachFifty: events.myEventHandler
Subscribing reachHundreds: events.myEventHandler
current counter: 10 counting reach Ten
current counter: 12 counting reach Dozen
current counter: 20 counting reach Ten
current counter: 24 counting reach Dozen
current counter: 30 counting reach Ten
current counter: 36 counting reach Dozen
current counter: 40 counting reach Ten
current counter: 48 counting reach Dozen
current counter: 50 counting reach Ten
current counter: 50 counting reach Fifty
current counter: 60 counting reach Ten
current counter: 60 counting reach Dozen
current counter: 70 counting reach Ten
current counter: 72 counting reach Dozen
current counter: 80 counting reach Ten
current counter: 84 counting reach Dozen
current counter: 90 counting reach Ten
current counter: 96 counting reach Dozen
current counter: 100 counting reach Ten
current counter: 100 counting reach Fifty
current counter: 100 counting reach Hundreds
0 comments:
Post a Comment