Problems with custom events

C

Claudio Grazioli

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."

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
 
A

Anthony Hughes

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
 
A

Anthony Hughes

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

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

Ant
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top