Click here to Skip to main content
Click here to Skip to main content

Creating a Custom Event Dictionary

By , 3 Jan 2007
 

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.

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)

About the Author

Daniel Dhillon
Web Developer
United States United States
Member
Currently working for a major international consulting firm.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralAn EventPool is a bit more comprehensive.protectorMarc Clifton4 Jan '07 - 1:25 
http://www.codeproject.com/csharp/EventPool.asp[^]
 
Marc
 

Thyme In The Country

People are just notoriously impossible. --DavidCrow
There's NO excuse for not commenting your code. -- John Simmons / outlaw programmer
People who say that they will refactor their code later to make it "good" don't understand refactoring, nor the art and craft of programming. -- Josh Smith


GeneralRe: An EventPool is a bit more comprehensive.memberleppie4 Jan '07 - 3:01 
Nevermind that! The framework provides System.ComponentModel.EventHandlerList!
 

GeneralRe: An EventPool is a bit more comprehensive.protectorMarc Clifton4 Jan '07 - 3:15 
leppie wrote:
framework provides System.ComponentModel.EventHandlerList!

 
D'Oh! | :doh: Of course!
 
Marc
 

Thyme In The Country

People are just notoriously impossible. --DavidCrow
There's NO excuse for not commenting your code. -- John Simmons / outlaw programmer
People who say that they will refactor their code later to make it "good" don't understand refactoring, nor the art and craft of programming. -- Josh Smith


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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 3 Jan 2007
Article Copyright 2007 by Daniel Dhillon
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid