Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

asking about event

146 views
Skip to first unread message

Kenichi

unread,
Mar 4, 2006, 6:46:35 PM3/4/06
to
hello, i'm still new to vs

i have a question,

how can i clear my event like to set that to null ?
i would like to do something like this without knowing
what is the event handler before
ex: (error)
button1.Click = null;

i know i can clear that manually using such as
button1.Click -= new EventHandler(button1_Click);

but in case there is many event assigned to button1.Click
i think it would be not effecient writing one by one line removing the event
handler
is there another way?

thanks in advance.


vj

unread,
Mar 4, 2006, 10:58:13 PM3/4/06
to
Hi Kenichi,

I was faced with a similar situation before, I was advised to use Reflection
to find out attached events to object, then you can examine this list and
remove them appropriately. If you are new, the reflection concept can be a
little overwhelming, but if you if have time on hand, I would say please
latch on to it. It is one concept that will help you a lot... Here is
sample..

http://www.vb-helper.com/howto_net_list_events.html

Ok ok.. this is c-sharp group, please excuse me for postin a vb sample..

VJ

"Kenichi" <nos...@nospam.com> wrote in message
news:OoNJhX%23PGH...@tk2msftngp13.phx.gbl...

Nick Hounsome

unread,
Mar 5, 2006, 2:26:39 AM3/5/06
to

"Kenichi" <nos...@nospam.com> wrote in message
news:OoNJhX%23PGH...@tk2msftngp13.phx.gbl...
> hello, i'm still new to vs
>
> i have a question,
>
> how can i clear my event like to set that to null ?

MyEvent = null;

> i would like to do something like this without knowing
> what is the event handler before
> ex: (error)
> button1.Click = null;

But this isn't your event it is Button's event (A member of Button COULD
write "Click = null").

The whole point of using event rather than a naked delegate is that it
restricts what can be done to it from outside the declaring class.

>
> i know i can clear that manually using such as
> button1.Click -= new EventHandler(button1_Click);
>
> but in case there is many event assigned to button1.Click
> i think it would be not effecient writing one by one line removing the
> event handler
> is there another way?

I cannot imagine a scenario in which the efficiency of adding or removing
event handlers from a button click event is a real issue.

It also seems unlikely to me that multiple handlers on the event is a good
design decision. I suspect that your best approach would be to have a single
handler that calls multiple methods.


Jon Skeet [C# MVP]

unread,
Mar 5, 2006, 3:34:50 AM3/5/06
to
Kenichi <nos...@nospam.com> wrote:
> hello, i'm still new to vs
>
> i have a question,
>
> how can i clear my event like to set that to null ?

You can't clear an event in another object - events themselves *only*
expose ways of subscribing and unsubscribing.

> i would like to do something like this without knowing
> what is the event handler before
> ex: (error)
> button1.Click = null;
>
> i know i can clear that manually using such as
> button1.Click -= new EventHandler(button1_Click);
>
> but in case there is many event assigned to button1.Click
> i think it would be not effecient writing one by one line removing the event
> handler is there another way?

Yes, that would probably be more efficient - very, very slightly, in a
way which would be insignificant to virtually all programs. However, it
would break the encapsulation of events.

If you're writing your own class which exposes an event, of course, you
could include a public method of ClearFooEventHandlers which set the
underlying delegate variable to null, if you want.

--
Jon Skeet - <sk...@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

0 new messages