Click here to Skip to main content
15,881,588 members
Articles / Programming Languages / C#

Creating a Custom Event Dictionary

Rate me:
Please Sign up or sign in to vote.
1.70/5 (9 votes)
3 Jan 2007CPOL 33.6K   252   10   3
An article on creating a custom event dictionary.

Introduction

This collection allows the developer to call any number of events from a centralized source.

Using the code

Simply instantiate the EventDictionary class as you would a regular Dictionary class, and pass in an EventHandler using the EventDictionary.Add method.

C#
using System;
using System.Collections.Generic;

/// <summary>
/// Represents a collection of key/event pairs that are accessible by the 
/// event name.
/// </summary>
public class EventDictionary : Dictionary<string, EventHandler>
{
    ...

    /// <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);
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) DoD
United States United States
Currently working for the Department of Defense at the AFN Broadcasting Center in California.

Comments and Discussions

 
GeneralAn EventPool is a bit more comprehensive. Pin
Marc Clifton4-Jan-07 1:25
mvaMarc Clifton4-Jan-07 1:25 
GeneralRe: An EventPool is a bit more comprehensive. Pin
leppie4-Jan-07 3:01
leppie4-Jan-07 3:01 
Nevermind that! The framework provides System.ComponentModel.EventHandlerList!


GeneralRe: An EventPool is a bit more comprehensive. Pin
Marc Clifton4-Jan-07 3:15
mvaMarc Clifton4-Jan-07 3:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.