Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / C#
Tip/Trick

Generic Event Handler Using Extension Method

Rate me:
Please Sign up or sign in to vote.
2.22/5 (4 votes)
31 Oct 2013CPOL 23.9K   4   20
A handy approach to raise events.

Introduction

Very often we need to invoke events, usually in Model classes , where we set some property and raise an event in order to notify any listeners. 

Using the code

Following is a full length example of 'How to use a Generic Event Handler to Raise an Event'.

It is good to have some helper (extension) method that can be used at various places, thus reducing the amount of code and making it quite maintainable.

C#
using System;
using ExtensionHelper;

namespace EventHandlers
{
    public class MyModel
    {
        private string _name;
        private int _age;
        private decimal _salary;

        MyModel() { }

        public string Name
        {
            get { return _name; }
            set 
            {
                _name = value;
                NameChanged.Raise(this, EventArgs.Empty);
            }
        }

        public int Age 
        { 
            get { return _age; } 
            set 
            {
                _age = value;
                AgeChanged.Raise(this, EventArgs.Empty);
            } 
        }

        public decimal Salary 
        { 
            get { return _salary; } 
            set 
            { 
                _salary = value;
                SalaryChanged.Raise(this, EventArgs.Empty);
            }
        }

        public EventHandler NameChanged;
        public EventHandler AgeChanged;
        public EventHandler SalaryChanged;

    }

}

namespace ExtensionHelper
{
    public static void Raise(this EventHandler eventHandler, object sender, EventArgs e)
    {
        if (eventHandler != null)
        {
             eventHandler(sender, e);
        }
    }

    public static void Raise<T>(this EventHandler<T> eventHandler, 
           object sender, T e) where T : EventArgs
    {
        if (eventHandler != null)
        {
            eventHandler(sender, e);
        }
    }
}

History

This is my first article on CodeProject, and I am using the above mentioned code in my current assignments.

License

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


Written By
Software Developer
India India
I am a Software Developer By Profession.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Athari1-Nov-13 5:25
Athari1-Nov-13 5:25 
GeneralMy vote of 1 Pin
johannesnestler1-Nov-13 0:49
johannesnestler1-Nov-13 0:49 
QuestionThere is also a possibility for null exception there due to a well known race condition when dealing with events Pin
Sacha Barber31-Oct-13 7:03
Sacha Barber31-Oct-13 7:03 
AnswerRe: There is also a possibility for null exception there due to a well known race condition when dealing with events Pin
Athari31-Oct-13 9:58
Athari31-Oct-13 9:58 
GeneralRe: There is also a possibility for null exception there due to a well known race condition when dealing with events Pin
johannesnestler1-Nov-13 0:14
johannesnestler1-Nov-13 0:14 
GeneralRe: There is also a possibility for null exception there due to a well known race condition when dealing with events Pin
Athari1-Nov-13 2:48
Athari1-Nov-13 2:48 
GeneralRe: There is also a possibility for null exception there due to a well known race condition when dealing with events Pin
johannesnestler1-Nov-13 4:44
johannesnestler1-Nov-13 4:44 
GeneralRe: There is also a possibility for null exception there due to a well known race condition when dealing with events Pin
Athari1-Nov-13 5:03
Athari1-Nov-13 5:03 
GeneralRe: There is also a possibility for null exception there due to a well known race condition when dealing with events Pin
johannesnestler1-Nov-13 5:53
johannesnestler1-Nov-13 5:53 
GeneralRe: There is also a possibility for null exception there due to a well known race condition when dealing with events Pin
Athari1-Nov-13 6:31
Athari1-Nov-13 6:31 
GeneralRe: There is also a possibility for null exception there due to a well known race condition when dealing with events Pin
johannesnestler1-Nov-13 8:34
johannesnestler1-Nov-13 8:34 
Hi Athari,

Now I'm confused... We are talking about events here, don't we? So yes, I was refering to topic 4, and never argued about topic 5 (if passed by value). This is the problem Sacha was refering to (his code is clear?)

Sorry about my (maybe to fast) opinion on Jon (as I mentioned, didn't know him and just read what he wrote on the linked site, sounded quite strange...)
You seem to be a good .NET programmer too, but I'm wondering why you insist that capturing an Event variable in a multithreaded scenario isn't needed. Still don't get it. Maybe I'm not that smart as I thought? Sigh | :sigh:

Btw. I'm programming for many years too, so value type and reference type diff is clear to me (or did you mean something else?

If I read my posts again, they sound quite rude, must say sorry for that (English is not my native language, as you may already know Wink | ;) )

Kind Regards
Johannes
AnswerRe: There is also a possibility for null exception there due to a well known race condition when dealing with events Pin
Bheeshm31-Oct-13 16:49
professionalBheeshm31-Oct-13 16:49 
QuestionNot following the convention... Pin
johannesnestler31-Oct-13 5:57
johannesnestler31-Oct-13 5:57 
AnswerRe: Not following the convention... Pin
OriginalGriff31-Oct-13 6:37
mveOriginalGriff31-Oct-13 6:37 
AnswerRe: Not following the convention... Pin
Bheeshm31-Oct-13 16:55
professionalBheeshm31-Oct-13 16:55 
GeneralRe: Not following the convention... Pin
johannesnestler1-Nov-13 0:46
johannesnestler1-Nov-13 0:46 
GeneralRe: Not following the convention... Pin
Bheeshm1-Nov-13 2:23
professionalBheeshm1-Nov-13 2:23 
GeneralRe: Not following the convention... Pin
johannesnestler1-Nov-13 5:00
johannesnestler1-Nov-13 5:00 
GeneralRe: Not following the convention... Pin
Athari1-Nov-13 5:20
Athari1-Nov-13 5:20 
GeneralRe: Not following the convention... Pin
johannesnestler1-Nov-13 5:59
johannesnestler1-Nov-13 5:59 

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.