Important alert: (current site time 7/15/2013 8:42:23 AM EDT)
 

article

Event Handler List

Email
Submitted on: 1/4/2007 8:58:29 AM
By: Daniel Dhillon  
Level: Intermediate
User Rating: Unrated
Compatibility: C#
Views: 6646
author picture
(About the author)
 
     I needed an event collection and I knew the simplest way would be to use a generic Dictionary class. I essentially removed the inherited Add method and created my own Add method to just accept an EventHandler argument. I called the base class passing in the EventHandler's event name as the key.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				

/// <summary>
///
Represents a collection of key/event pairs that are accessible by the
/// event name.
/// </summary>
public class EventDictionary : Dictionary<string, EventHandler>
{
      /// <summary>
      /// Initializes a new instance of the EventDictionary class.
      /// </summary>
      public EventDictionary() : base()
      {
            // Default constructor
      }
      
      /// <summary>
      /// DO NOT USE
      /// </summary>
      [EditorBrowsable(EditorBrowsableState.Never)]
      public new void Add(string key, EventHandler value)
      {
            throw new Exception("The method or operation is not to be used.");
      }
      
      /// <summary>
      /// Adds an entry with the specified value into the EventDictionary 
      /// collection with the EventHandler's name as the key. 
      /// </summary>
      /// <param name="e">The value of the event to add.  This value cannot 
      /// be null (Nothing in Visual Basic).</param>
      public void Add(EventHandler e)
      {
            if (e == null)

throw new ArgumentException("Event Handler cannot be null."
"e");

            string s = e.Target.GetType().GetEvents()[0].ToString();
            base.Add(s.Substring(s.LastIndexOf(" ") + 1), e);
      }
}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.