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...
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.
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.
You may link to this article from another website, but ONLY if it is not wrapped in a frame.
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> publicclassEventDictionary : 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)] publicnewvoid Add(string key, EventHandler
value)
{ thrownewException("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> publicvoid Add(EventHandler
e)
{ if (e == null)
thrownewArgumentException("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
Your Vote
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.)