424,303 Members | 1,339 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,303 IT Pros & Developers. It's quick & easy.

prevent duplicate event handlers

P: n/a
Anyone know, in VB.NET (2005) how to prevent duplicate occurrences of the
same event handler on an event?

e.g... I have some object which raises an event, and some other class which
consumes them. So in essence what I want to do is something like this in the
consumer class...

If Not objEventSource.SomeEvent.Handlers.Contains(Address Of me.EventHandler)
Then _
AddHandler objEventSource.SomeEvent, AddressOf me.EventHandler
Dec 3 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
>If Not objEventSource.SomeEvent.Handlers.Contains(Address Of me.EventHandler)
>Then _
AddHandler objEventSource.SomeEvent, AddressOf me.EventHandler
Can't you just keep a boolean the specifies whether or not the handler
has been added already?
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Dec 3 '07 #2

P: n/a
Arthur Dent wrote:
Anyone know, in VB.NET (2005) how to prevent duplicate occurrences of
the same event handler on an event?
Short answer - you can't.

The delegate/event mechanism is /so/ fundamental to the .Net "event"
model that I have to ask - /why/ do you feel the need to do this?

Regards,
Phill W.
Dec 3 '07 #3

P: n/a
On Dec 2, 8:31 pm, "Arthur Dent" <hitchhikersguideto-n...@yahoo.com>
wrote:
Anyone know, in VB.NET (2005) how to prevent duplicate occurrences of the
same event handler on an event?

e.g... I have some object which raises an event, and some other class which
consumes them. So in essence what I want to do is something like this in the
consumer class...

If Not objEventSource.SomeEvent.Handlers.Contains(Address Of me.EventHandler)
Then _
AddHandler objEventSource.SomeEvent, AddressOf me.EventHandler
Have tried declaring an event using the Custom keyword?

Public Custom Event Foo As EventHandler

AddHandler(ByVal value As EventHandler)
' Add logic here to prevent duplicate handlers.
End AddHandler

RemoveHandler(ByVal value As EventHandler)

End RemoveHandler

RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)

End RaiseEvent

End Event
Dec 3 '07 #4

P: n/a
Well, what I have is an event consumer class that is a strongly-typed
collection type. and it uses one event handler, dynamically assigned, to
listen to the same event of all the items contained in the collection - I
wanted to make sure that if the user added a second ref instance of an
object to the collection, it would not attach a second instance of the event
handler to that new instance.

I worked around it I guess, by simply adding code prohibiting them from
adding the same object to the collection more than once. It works anyway.

Stephany - attaching an event handler twice _will_ in fact, fire it twice. I
tested this by manually attaching a button click handler on a winform twice,
and putting a simple indicator in the onclick code. Clicking the button once
then runs the indicator code twice.
"Phill W." <p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-kwrote in message
news:fj**********@south.jnrs.ja.net...
Arthur Dent wrote:
>Anyone know, in VB.NET (2005) how to prevent duplicate occurrences of the
same event handler on an event?

Short answer - you can't.

The delegate/event mechanism is /so/ fundamental to the .Net "event" model
that I have to ask - /why/ do you feel the need to do this?

Regards,
Phill W.
Dec 3 '07 #5

P: n/a
Wow! That "Custom" keyword and event framework is pretty cool!
For posterity, here is a good link explaining it in depth:
http://www.simple-talk.com/content/a...px?article=224

I will have to remember that... although it won't work for this instance,
because I need to prevent duplicates in the event consumer, not the source.

Thanks for the great info though! :)
"Brian Gideon" <br*********@yahoo.comwrote in message
news:0b**********************************@v4g2000h sf.googlegroups.com...
On Dec 2, 8:31 pm, "Arthur Dent" <hitchhikersguideto-n...@yahoo.com>
wrote:
>Anyone know, in VB.NET (2005) how to prevent duplicate occurrences of the
same event handler on an event?

e.g... I have some object which raises an event, and some other class
which
consumes them. So in essence what I want to do is something like this in
the
consumer class...

If Not objEventSource.SomeEvent.Handlers.Contains(Address Of
me.EventHandler)
Then _
AddHandler objEventSource.SomeEvent, AddressOf me.EventHandler

Have tried declaring an event using the Custom keyword?

Public Custom Event Foo As EventHandler

AddHandler(ByVal value As EventHandler)
' Add logic here to prevent duplicate handlers.
End AddHandler

RemoveHandler(ByVal value As EventHandler)

End RemoveHandler

RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)

End RaiseEvent

End Event
Dec 3 '07 #6

P: n/a
See my earlier follow-up.
"Arthur Dent" <hi*********************@yahoo.comwrote in message
news:74**********************************@microsof t.com...
Well, what I have is an event consumer class that is a strongly-typed
collection type. and it uses one event handler, dynamically assigned, to
listen to the same event of all the items contained in the collection - I
wanted to make sure that if the user added a second ref instance of an
object to the collection, it would not attach a second instance of the
event handler to that new instance.

I worked around it I guess, by simply adding code prohibiting them from
adding the same object to the collection more than once. It works anyway.

Stephany - attaching an event handler twice _will_ in fact, fire it twice.
I tested this by manually attaching a button click handler on a winform
twice, and putting a simple indicator in the onclick code. Clicking the
button once then runs the indicator code twice.
"Phill W." <p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-kwrote in message
news:fj**********@south.jnrs.ja.net...
>Arthur Dent wrote:
>>Anyone know, in VB.NET (2005) how to prevent duplicate occurrences of
the same event handler on an event?

Short answer - you can't.

The delegate/event mechanism is /so/ fundamental to the .Net "event"
model that I have to ask - /why/ do you feel the need to do this?

Regards,
Phill W.
Dec 3 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.