Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 25th, 2008, 03:19 AM
Authorized User
Join Date: Feb 2007
Location: , , USA.
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default Event Handling C# 2.0

Hi All,

I am working on a project that requires handling of custom events. Some class will raise the DataEvent of RaiseEventClass. I have to implement event handler for the DataEvent in the RaiseEventClass. Please help me how to implement event handler. Please show some codes if possible.
Below is the skeleton of the class.
Thanks in advance!

public class RaiseEventClass
    public delegate void RaiseEventDelegate(ArrayList files);
    public event RaiseEventDelegate DataEvent;

    public void SearchFiles(ArrayList list)
       DataEvent(list); //raising event for testing


  #2 (permalink)  
Old January 25th, 2008, 04:39 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts

You should check for null when raising events in C#

if( DataEvent != null ) DataEvent(list);

Also, usually event delegates follow the format of:

void EventName(object sender, EventArgs e)

where e is either the EventArgs class or some class that derives from it. In this case something like the following is best:

  public class DataEventArgs : EventArgs
     private ArrayList _list;
     public ArrayList FileList { get { return _list; } set { _list = value; } }
  public void DataEventEventHandler(object sender, DataEventArgs e)
  public event DataEventEventHandler DataEvent;

  public void SearchFiles(ArrayList list)
    DataEventArgs e = new DataEventArgs();
    e.FileList = list;

    if( DataEvent != null ) DataEvent(this, e); //raising event for testing
To consume the event you then do the usual:

RaiseEventClass c = new RaiseEventClass();
c.DataEvent += new DataEventEventHandler(My_DataEvent);

//elsewhere define the method
public void My_DataEvent(object sender, DataEventArgs e)
   // Do something with the list...
   // e.FileList

/- Sam Judson : Wrox Technical Editor -/
  #3 (permalink)  
Old January 25th, 2008, 11:37 AM
Friend of Wrox
Join Date: Jun 2003
Location: , , USA.
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts

Don't forget the delegate...:)

public void DataEventEventHandler(object sender, DataEventArgs e)
should be:

public delegate void DataEventEventHandler(object sender, DataEventArgs e);
Also just wanted to add using the DataEventArgs constructor and the OnEventName convention in the event publisher class:

public class DataEventArgs : EventArgs {

        private ArrayList _list;

        public ArrayList FileList 
            get { return _list; } 
            set { _list = value; }

        public DataEventArgs(ArrayList list)
            this._list = list;

class EventPublisher {

        public event DataEventEventHandler DataEvent;

        public void CreateList() 
            ArrayList list = new ArrayList();

        private void SearchFiles(ArrayList list) {

            DataEventArgs e = new DataEventArgs(list);

        protected virtual void OnDataEvent(DataEventArgs e)
            if (DataEvent != null)
                DataEvent(this, e);

class EventSubscriber {

        static void Main() 
            EventPublisher eventPublisher = new EventPublisher();
            eventPublisher.DataEvent += new DataEventEventHandler(eventPublisher_DataEvent);


        static void eventPublisher_DataEvent(object sender, DataEventArgs e) {




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Event Handling Apocolypse2005 Visual Basic 2005 Basics 1 March 10th, 2009 07:14 AM
Event handling in c# bhavna VS.NET 2002/2003 1 March 7th, 2007 12:19 PM
Event handling in c# bhavna General .NET 1 March 7th, 2007 05:32 AM
Event Handling Bill Crawley ASP.NET 2.0 Basics 0 February 28th, 2007 07:32 AM
Help with Event handling nulogix Java GUI 1 October 11th, 2004 03:55 PM

All times are GMT -4. The time now is 05:34 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.