Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 25th, 2008, 03:19 AM
Authorized User
 
Join Date: Feb 2007
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
    }

}

A.Asif
__________________
A.Asif
 
Old January 25th, 2008, 04:39 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

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:

Code:
  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:

[code]
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 -/
 
Old January 25th, 2008, 11:37 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Don't forget the delegate...:)

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

Code:
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:

Code:
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();
            list.Add("Test");
            SearchFiles(list);
        }

        private void SearchFiles(ArrayList list) {

            DataEventArgs e = new DataEventArgs(list);
            this.OnDataEvent(e);
        }

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

            eventPublisher.CreateList();
        }

        static void eventPublisher_DataEvent(object sender, DataEventArgs e) {
            Console.WriteLine(e.FileList[0]);
            Console.Read();
        }
    }

Best,

Bob










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





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