Click here to Skip to main content
Licence CPOL
First Posted 10 Oct 2006
Views 12,687
Downloads 94
Bookmarked 10 times

Simplest event delegate ever

By | 10 Oct 2006 | Article
The simplest sample of how an event delegate can be used in C#.

Introduction

Check out this simple event delegate code. First, you have a source which generates an event:

using System;
using System.Collections.Generic;
using System.Text;

namespace eventDelegate
{
    class Source
    {
        public event EventHandler SomeEvent; 
        // this event is of type EventHandler, 
        // meaning it can notify any function whose signature is like the 
        // one of EventHandler EventHandler is the delegate

        public void RaiseEvent()
        {
            if (null != SomeEvent) // to avoid exceptions when event 
            {                      // delegate wiring is not done
                SomeEvent(this, null);
            }
        }
    }
}

Next, you have a receiver which is supposed to get a notification of the event:

using System;
using System.Collections.Generic;
using System.Text;

namespace eventDelegate
{
    class Receiver
    {
        public void NotifyMe(object source, EventArgs e)
        {
            Console.WriteLine("I am notified by "+source.GetType());
        }
    }
}

Then you have the main program which does the wiring:

using System;
using System.Collections.Generic;
using System.Text;

namespace eventDelegate
{
    class Program
    {
        static void Main(string[] args)
        {
            Source theSource = new Source();
            Receiver theReceiver = new Receiver();

            theSource.SomeEvent += new EventHandler(theReceiver.NotifyMe);

            theSource.RaiseEvent();
        }
    }
}

As simple as that. It can now be extended to include custom event data and therefore a new class inheriting from EventArgs and therefore ending up with a new delegate which can handle this new EventArgs.

License

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

About the Author

nitstheone



India India

Member



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. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralGreat! PinmemberHatrick3:34 9 Jan '07  
GeneralYou may want to reference my article PinmemberTodd Wilder8:21 16 Oct '06  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 10 Oct 2006
Article Copyright 2006 by nitstheone
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid