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 / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

Ahh not again!

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Johnny E. Jensen - 21 Sep 2007 20:51 GMT
Hello

I have in a small application a usercontrol with 4 buttons. Btn1, Btn2, Btn3
and Btn4 these button is placed at the buttom of the usercontrol. On top of
the usercontrol i place an anothe usercontrol depends on the buttons i click
on. A little bit like the Outlook Side navigator.

In the usercontrols (the above) i have some label controls and some other
stuff, but when I click on one of the label controls, I want to raise an
event - CustomerCreate

That event i made by using:

public delegate void CustomerCreateClickHandler();
public event CustomerCreateClickHandler CustomerCreateClick;

In the click event on the label i use:

If ( CustomerCreateClick != null )
{
   CustomerCreateClick();
}
to raise the event. I'll use this approach in btn1 - btn 3 and it works just
fine. Now on the btn4 the CustomerCreateClick is always Null?????

Can anyone explain to my the life cycle for a delegate and when it is
created on the control - or if there is another approach for creating
events/delegates.

Kind Regards
Johnny E. Jensen
Peter Duniho - 21 Sep 2007 21:05 GMT
> [...]
> Can anyone explain to my the life cycle for a delegate and when it is
> created on the control - or if there is another approach for creating
> events/delegates.

A delegate has the same lifetime as any other object.  If something has
a reference to the delegate, it will continue to exist.  Once no more
references to the delegate exist, it is eligible for garbage collection
and at some point in the future may be released.

This doesn't have anything to do with why an event you've declared
remains null.  If you'd actually subscribed a delegate to the event, it
would remain subscribed until you unsubscribe it.

So either you haven't subscribed to the event, or you have subsequently
unsubscribed the delegate from that event.  Whatever delegate you create
to subscribe to the event will exist for as long as you have a reference
to the delegate; if the delegate is subscribed to an event, then the
event has a reference to the delegate and the delegate will live as long
as it remains subscribed.

Pete
Jon Skeet [C# MVP] - 21 Sep 2007 21:08 GMT
<snip>

> Can anyone explain to my the life cycle for a delegate and when it is
> created on the control - or if there is another approach for creating
> events/delegates.

CustomerCreateClick will be null unless something subscribes to the
event.

Could you post a short but complete program which demonstrates the
problem?

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

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Cor Ligthert[MVP] - 22 Sep 2007 06:40 GMT
Hi Johnny,

Why not use a normal method in your user control that handles your event, to
which you point four events and use the button name using a cast of the
sender. That is all you have to do?

Cor

> Hello
>
[quoted text clipped - 27 lines]
> Kind Regards
> Johnny E. Jensen
Johnny E. Jensen - 22 Sep 2007 08:59 GMT
Hello

Thanks Peter, Jon and Cor

I think i'll got it right here. First i create a new instance of the
UserControl1 : objNavEmployee = new UserControl1();
The event on the objNavEmployee.CreateEmployeeClick is still null - right?
Now i add a reference to that event on the form like
objNavEmployee.CreateEmployeeClick +=
UserControl1.CreateEmployeeClickHandler(ClickEvent);

The event on the objNavEmployee is not null - and for the first 3 buttons i
just did that. But on the 4 button I'll also did the above but at the moment
i added the UserControl i'll created a new instance and then this instances
CreateEmployeeClick event is still null besause the event has not been
referenced.

I have struggled with this a lot of times - now i know what to do - so
thanks again.

Kind regards
Johnny E. Jensen

> Hello
>
[quoted text clipped - 27 lines]
> Kind Regards
> Johnny E. Jensen

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.