Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / April 2005

Tip: Looking for answers? Try searching our database.

Problems with custom events

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Anthony Hughes - 31 Mar 2005 14:39 GMT
Browsing through some online tutorials, I've been tryingto add a custom event
to an app I'm developing but I'm having a little problem.

Simplifying the code down to the following (a form with a button that fires
the event):

namespace CustomEvent
{
       public delegate void MyEventHandler(object sender, MyEventArgs e);

       /// <summary>
       /// Summary description for Form1.
       /// </summary>
       public class Form1 : System.Windows.Forms.Form
       {
               public event MyEventHandler MyEvent;

               protected virtual void OnMyEvent(MyEventArgs e)
               {
                       MyEvent(this, e);
               }

<< snip template form code >>

               private void button1_Click(object sender, System.EventArgs e)
               {
                       OnMyEvent(new MyEventArgs("Fred"));
               }
       }

       public class MyEventArgs: EventArgs
       {
               private string name;
               public MyEventArgs(string name)
               {
                       this.name = name;
               }
               public string Name
               {
                       get {return name;}
               }
       }
}

The problem I'm having is that the MyEvent(this, e); call fails in
OnMyEvent(...) with the following error:

"An unhandled exception of type 'System.NullReferenceException' occurred in
CustomEvent.exe

Additional information: Object reference not set to an instance of an object."

Can anyone explain what I'm doing wrong?

Cheers in advance
Ant
Claudio Grazioli - 31 Mar 2005 15:18 GMT
> Simplifying the code down to the following (a form with a button that fires
> the event):
[quoted text clipped - 44 lines]
>
> Additional information: Object reference not set to an instance of an object."

You can only fire an event, if someone is listening to it. So if nowhere is
some code that subscribed to your event like:
 form1.MyEvent += new Eventhandler(dosomething);
you can't fire your event. How do you no, if someone subscribed to your
event? Simply by checking for null:

protected virtual void OnMyEvent(MyEventArgs e)
{
 if (MyEvent != null)
   MyEvent(this, e);
}

hth

Signature

Claudio Grazioli
http://www.grazioli.ch

Anthony Hughes - 01 Apr 2005 08:39 GMT
>> Simplifying the code down to the following (a form with a button that fires
>> the event):
[quoted text clipped - 59 lines]
>
>hth

Thanks Claudio - seems so obvious to me now! Doh!

Ant

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.