Bittle

Event Handlers (C#)

Oct 12th, 2016
164
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.44 KB | None | 0 0
  1. //  10/12/2016
  2. //  Class: C#
  3.  
  4. //  Events:
  5. //      Publisher                               Subscriber
  6. //      - publishes events                      - subscribes to event
  7.  
  8. //  =============================== EXAMPLE CODE BELOW ===============================
  9.  
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14.  
  15. namespace MyCollections
  16. {
  17.     //We inherit from ArrayList to create an array list which also
  18.     //publishes an event.
  19.     public class ArrayListWithEvent : System.Collections.ArrayList
  20.     {
  21.         /////////////////////////
  22.         //Publishing the event!!!
  23.         /////////////////////////
  24.         public event EventHandler Changed;
  25.  
  26.         //The goal here is to wrap event "invocation" behavior. This is
  27.         //important so that classes which inherit ArrayListWithEvent can
  28.         //override this method with custom invocation behavior.
  29.         protected virtual void OnChanged(EventArgs e)
  30.         {
  31.             //The variable "Changed" is an EventHandler. It's sort of a "collection"
  32.             //of subscribers to the event. Changed != null means that we have subscribers;
  33.             //Changed == null means we have no subscribers.
  34.             if (Changed != null)
  35.             {
  36.                 Changed(this, e);
  37.             }
  38.         }
  39.  
  40.         //Override Add method of array list, so we can fire event when user adds to list
  41.         //OnChanged is how we fire the event.
  42.         //
  43.         //Note: EventArgs is a way to pass event data; for example, here we could pass a
  44.         //string "add". Then, we could also override the Remove method of the ArrayList
  45.         //so that it fires an event which passes a string "remove" to EventArgs.
  46.         //Then, in the OnChanged method, we can handle these two cases differently.
  47.         //In this example, we leave EventArgs empty.
  48.         public override int Add(object value)
  49.         {
  50.             OnChanged(EventArgs.Empty);
  51.  
  52.             //This line here says "do what my parent (ArrayList) does".
  53.             //i.e., we want Add to act the same as ArrayList, so we call
  54.             //the original method.
  55.             return base.Add(value);
  56.         }
  57.     }
  58.  
  59.  
  60.     ///////////////////////////
  61.     //Subscribing to the event!
  62.     ///////////////////////////
  63.  
  64.     //"event listeners" are helper classes which encapsulate the
  65.     //methods for subscribing to an event.
  66.     //Typically, the constructor subscribes to the event, and also
  67.     //a Detach() method is included, which unsubscribes from the event.
  68.     public class ListChangedEventListener
  69.     {
  70.         private ArrayListWithEvent list;
  71.  
  72.         public ListChangedEventListener(ArrayListWithEvent inputList)
  73.         {
  74.             list = inputList;
  75.             //This is the key statement: This subscribes the ListChanged method
  76.             //to the event Changed. Remember, Changed is like a "collection" of events.
  77.             //"+=" is used to add ListChanged to the Changed event handler.
  78.             //Then, ListChanged will be called when Changed fires.
  79.             list.Changed += ListChanged;
  80.         }
  81.  
  82.         //The method ListChanged, which we are subscribing to Changed in the constructor of this class.
  83.         //The method here must have this return type and parameters to match the Changed event; you can
  84.         //use delegates to define a custom event handler with different return types and parameters, but
  85.         //we used the default event handler when we published the event.
  86.         public void ListChanged(object sender, EventArgs e)
  87.         {
  88.             Console.WriteLine("User has added to list.");
  89.         }
  90.  
  91.         public void Detach()
  92.         {
  93.             //Remove ListChanged from Changed event handler
  94.             list.Changed -= new ChangedEventHandler(ListChanged);
  95.             //Delete the list so we can reuse the same listener instance, if we want.
  96.             list = null;
  97.         }
  98.     }
  99.  
  100.    
  101.  
  102.     class Program
  103.     {
  104.         static void Main(string[] args)
  105.         {
  106.             //When we construct an object of type ArrayListWithEvent, it publishes the event.
  107.             ArrayListWithEvent list1 = new ArrayListWithEvent();
  108.            
  109.             //When we construct the listener object, it subscribes list1 to the event.
  110.             ListChangedEventListener listener = new ListChangedEventListener(list1);
  111.  
  112.             list1.Add(6);
  113.             list1.Add(7);
  114.  
  115.             //Unsubscribe.
  116.             listener.Detach();
  117.             list1.Add(6);
  118.  
  119.         }
  120.     }
  121. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment