[Delegates and Events]

J

Johnny E Jensen

Hi
Using C#, VS2005 .NET2

I have a custom usercontrol.
Here i have a picturebox (Close) and a CheckedListBox

A public delegate void for CloseControlHandler();, and event for that
CloseControlEvent;
A public delegate void for AddFieldToListHandler(string tablename, object
item); and event for that AddFieldToListEvent;

When the user clicks the picture, the picturebox.click event fires. Here I
raises my CloseControlEvent, this allways works fine.
When the user clicks on a item in the CheckListBox the
CheckListBox.ItemCheck is fired (besause the CheckOnClick property = true),
I call a method to raise my event AddFieldToListEvent, but always i'll get
the 'Object reference not set....' Error. it seems that the
AddFieldToListEvent i allways NULL.
I do use the:
If ( AddFieldToListEvent != null ) to prevent the application to crash.

Could anyone provide with some hints.

The Code:
public delegate void AddFieldToListHandler(string tablename,
FieldDescriptionClass description);
public event AddFieldToListHandler AddFieldToListEvent;

public delegate void CloseControlHandler(Control ClosingControl);
public event CloseControlHandler CloseControlEvent;

private void picClose_Click(object sender, EventArgs e)
{
if ( CloseControlEvent !=null)
{
CloseControlEvent(this);
}
}

private void lstFields_ItemCheck(object sender, ItemCheckEventArgs e)
{
RaiseAddEvent(_strTablename, _objField);
}

private void RaiseAddEvent(string tablename, FieldDescriptionClass
description)
{
if ( AddFieldToListEvent != null) // this AddFieldToListEvent is always
null ?????????
AddFieldToListEvent(tablename, description);
}

Kind Regards
Johnny E Jensen
 
J

Jon Skeet [C# MVP]

Johnny E Jensen wrote:

Could anyone provide with some hints.

It's not clear why you expect the event *not* to be null. Instead of
providing partial code, could you provide a short but complete program
which demonstrates the problem?

See http://www.pobox.com/~skeet/csharp/complete.html for what I mean by
that.

In addition, if you're storing a handler separately to the event, you
would normally implement the add/remove behaviour by hand to use that
handler delegate. It's possible that I've misunderstood the point of
the separate delegate/event due to only seeing part of the code though.

See http://www.pobox.com/~skeet/csharp/events.html for more details of
this.

Jon
 
J

Jon Skeet [C# MVP]

Johnny E Jensen wrote:

Could anyone provide with some hints.

It's not clear why you expect the event *not* to be null. Instead of
providing partial code, could you provide a short but complete program
which demonstrates the problem?

See http://www.pobox.com/~skeet/csharp/complete.html for what I mean by
that.

In addition, if you're storing a handler separately to the event, you
would normally implement the add/remove behaviour by hand to use that
handler delegate. It's possible that I've misunderstood the point of
the separate delegate/event due to only seeing part of the code though.

See http://www.pobox.com/~skeet/csharp/events.html for more details of
this.

Jon
 
D

Dave Sexton

Hi Johnny,

You should consider following Microsoft's standards for event design:

".NET Framework Developer's Guide, Event Design"
http://msdn2.microsoft.com/en-us/library/ms229011.aspx

Also, in the 2.0 framework you no longer need to create custom delegates.
Instead, you can use the generic EventHandler<TEventArgs> delegate if you
need to use custom event arguments. Create your own class that derives from
System.EventArgs and use it for the TEventArgs parameter.
 

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