Hi ZAky
Don call the delegate if it is null.
if(DoThis != null)
DoThis(this, new EventArgs());
Anyways I suggesto to stick with the design pattern microsoft uses
throughout WindowsForms framework
//Class for event releated data
public class SomethingHappenEventArgs: EventArgs
{
.....
//Event data goes here
}
//Delegate for the event
public delegate void SomethingHappenEventHandler(object sender,
SomeEventEventArgs e);
class Foo
{
public SomethingHappenEventHandler SomethingHappen;
protected virtual OnSomethingHappen(SomethingHappenEventArgs e)
{
if(SomethingHappen != null)
SomethingHappen(this, e);
}
}
class inheritors can handle the event by overriding the OnXXX method and at
the same time block the event from firing. Other methods can raise the event
by calling the OnXXX method.
There is an article of best practices for declaring events in MSDN
--
HTH
Stoitcho Goutsev (100) [C# MVP]
"ZAky" <ZA**@discussions.microsoft.com> wrote in message
news:9E**********************************@microsof t.com...
What should I do?
I don't need the class that raise the event to hook the event.
I want classes that use this class to hook the event.
Thank you
ZAky
"Stoitcho Goutsev (100) [C# MVP]" wrote:
Hi ZAky,
This is not EventArgs that thorws the exception.
Your event is not hooked up. In this case the deleagte class member that
holds the chain of event hadlers is not initialized and is null. So,
using that not initilized reference is what cause the exception
....
DoThis(this, new EventArgs());
//DoThis is null
....
--
HTH
Stoitcho Goutsev (100) [C# MVP]
"ZAky" <ZA**@discussions.microsoft.com> wrote in message
news:8D**********************************@microsof t.com... Why do I get a System.NullReferenceException for this script?
I added the following script to a simple form with a button.
<Script>
public event EventHandler DoThis;
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
OnDoThis();
}
protected void OnDoThis()
{
//The DoThis EventArgs is not recognize at run time
//This and new EventArgs() are fine.
DoThis(this, new EventArgs());
}
</Script>
Thank you