Ahh not again!

J

Johnny E. Jensen

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
 
P

Peter Duniho

Johnny said:
[...]
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
 
J

Jon Skeet [C# MVP]

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

Cor Ligthert[MVP]

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
 
J

Johnny E. Jensen

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
 

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