PC Review


Reply
Thread Tools Rate Thread

[Delegates and Events]

 
 
Johnny E Jensen
Guest
Posts: n/a
 
      15th Dec 2006
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


 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      15th Dec 2006
Johnny E Jensen wrote:

<snip>

> 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

 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      15th Dec 2006
Johnny E Jensen wrote:

<snip>

> 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

 
Reply With Quote
 
Dave Sexton
Guest
Posts: n/a
 
      15th Dec 2006
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.

--
Dave Sexton

"Johnny E Jensen" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why events if we have delegates puzzlecracker Microsoft C# .NET 3 28th Oct 2008 10:25 PM
Custom Events versus Multicast Delegates for events(VB.NET) =?Utf-8?B?aGVyYmVydA==?= Microsoft Dot NET 2 14th Feb 2007 02:51 PM
Advanced use of delegates to automatically disconnect delegates from a set of events kristian.freed@gmail.com Microsoft C# .NET 2 18th Nov 2006 09:56 PM
Events and delegates Sgt. Sausage Microsoft C# .NET 6 13th Sep 2004 10:01 PM
Events and delegates. bredal Jensen Microsoft ASP .NET 7 30th Apr 2004 09:04 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:08 AM.