473,324 Members | 2,248 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Removing Event Handlers Question - Rephrased

I think I asked the wrong question last time, so I am starting a separate
post to distinguish them.

Take five classes: ClassA and ClassW...Z

ClassA raises five events: Event1...5

ClassW...Z handle some combination of these events. For example

ClassW handles Event1, 2 and 5
ClassX handles Event1, 3 and 4
ClassY handles Event2 and 4
...

At some point, I want to detach ClassX from ClassA, so if I were to write
the code longhand, I would have

RemoveHandler ClassA.Event1, AddressOf ClassX.Event1Handler
RemoveHandler ClassA.Event3, AddressOf ClassX.Event3Handler
RemoveHandler ClassA.Event4, AddressOf ClassX.Event4Handler

This is not so bad for a small number of classes and events, but I actually
have about 30 classes and 50 events. The combinations are too large to
contemplate the longhand approach.

..NET must keep track of what event is linked to what handler somewhere so
that it knows what to call. I am looking for a generic (iterative?) way to
detach handlers from events, given only the source class and target class.
These classes implement interfaces that contain the events and handlers if
that helps.

Can anyone give me any clues?

TIA

Charles
Nov 20 '05 #1
5 1544
If you declare your classes using the WithEvents keyword, you can then
remove the all of the handlers by setting the class to nothing.

Example: Dim WithEvents cl as New ClassW

cl = Nothing

"Charles Law" <bl***@nowhere.com> wrote in message
news:O7**************@TK2MSFTNGP09.phx.gbl...
I think I asked the wrong question last time, so I am starting a separate
post to distinguish them.

Take five classes: ClassA and ClassW...Z

ClassA raises five events: Event1...5

ClassW...Z handle some combination of these events. For example

ClassW handles Event1, 2 and 5
ClassX handles Event1, 3 and 4
ClassY handles Event2 and 4
...

At some point, I want to detach ClassX from ClassA, so if I were to write
the code longhand, I would have

RemoveHandler ClassA.Event1, AddressOf ClassX.Event1Handler
RemoveHandler ClassA.Event3, AddressOf ClassX.Event3Handler
RemoveHandler ClassA.Event4, AddressOf ClassX.Event4Handler

This is not so bad for a small number of classes and events, but I actually have about 30 classes and 50 events. The combinations are too large to
contemplate the longhand approach.

.NET must keep track of what event is linked to what handler somewhere so
that it knows what to call. I am looking for a generic (iterative?) way to
detach handlers from events, given only the source class and target class.
These classes implement interfaces that contain the events and handlers if
that helps.

Can anyone give me any clues?

TIA

Charles

Nov 20 '05 #2
Take a look at this, it might help.

http://www.vbinfozine.com/t_bindevt.shtml


"Charles Law" <bl***@nowhere.com> wrote in message
news:O7**************@TK2MSFTNGP09.phx.gbl...
I think I asked the wrong question last time, so I am starting a separate
post to distinguish them.

Take five classes: ClassA and ClassW...Z

ClassA raises five events: Event1...5

ClassW...Z handle some combination of these events. For example

ClassW handles Event1, 2 and 5
ClassX handles Event1, 3 and 4
ClassY handles Event2 and 4
...

At some point, I want to detach ClassX from ClassA, so if I were to write
the code longhand, I would have

RemoveHandler ClassA.Event1, AddressOf ClassX.Event1Handler
RemoveHandler ClassA.Event3, AddressOf ClassX.Event3Handler
RemoveHandler ClassA.Event4, AddressOf ClassX.Event4Handler

This is not so bad for a small number of classes and events, but I actually have about 30 classes and 50 events. The combinations are too large to
contemplate the longhand approach.

.NET must keep track of what event is linked to what handler somewhere so
that it knows what to call. I am looking for a generic (iterative?) way to
detach handlers from events, given only the source class and target class.
These classes implement interfaces that contain the events and handlers if
that helps.

Can anyone give me any clues?

TIA

Charles

Nov 20 '05 #3
Hi

Thanks for the suggestion. I have just tried it though and get the same
result: after disposing the control and setting it to nothing, I can raise
event and the handler still handles it.

Charles
"yEaH rIgHt" <nospam@haha> wrote in message
news:10*************@corp.supernews.com...
If you declare your classes using the WithEvents keyword, you can then
remove the all of the handlers by setting the class to nothing.

Example: Dim WithEvents cl as New ClassW

cl = Nothing

"Charles Law" <bl***@nowhere.com> wrote in message
news:O7**************@TK2MSFTNGP09.phx.gbl...
I think I asked the wrong question last time, so I am starting a separate post to distinguish them.

Take five classes: ClassA and ClassW...Z

ClassA raises five events: Event1...5

ClassW...Z handle some combination of these events. For example

ClassW handles Event1, 2 and 5
ClassX handles Event1, 3 and 4
ClassY handles Event2 and 4
...

At some point, I want to detach ClassX from ClassA, so if I were to write the code longhand, I would have

RemoveHandler ClassA.Event1, AddressOf ClassX.Event1Handler
RemoveHandler ClassA.Event3, AddressOf ClassX.Event3Handler
RemoveHandler ClassA.Event4, AddressOf ClassX.Event4Handler

This is not so bad for a small number of classes and events, but I

actually
have about 30 classes and 50 events. The combinations are too large to
contemplate the longhand approach.

.NET must keep track of what event is linked to what handler somewhere so that it knows what to call. I am looking for a generic (iterative?) way to detach handlers from events, given only the source class and target class. These classes implement interfaces that contain the events and handlers if that helps.

Can anyone give me any clues?

TIA

Charles


Nov 20 '05 #4
That is absolutely, 100%, spot on, what I was after.

Cheers.

Charles
"yEaH rIgHt" <nospam@haha> wrote in message
news:10*************@corp.supernews.com...
Take a look at this, it might help.

http://www.vbinfozine.com/t_bindevt.shtml


"Charles Law" <bl***@nowhere.com> wrote in message
news:O7**************@TK2MSFTNGP09.phx.gbl...
I think I asked the wrong question last time, so I am starting a separate post to distinguish them.

Take five classes: ClassA and ClassW...Z

ClassA raises five events: Event1...5

ClassW...Z handle some combination of these events. For example

ClassW handles Event1, 2 and 5
ClassX handles Event1, 3 and 4
ClassY handles Event2 and 4
...

At some point, I want to detach ClassX from ClassA, so if I were to write the code longhand, I would have

RemoveHandler ClassA.Event1, AddressOf ClassX.Event1Handler
RemoveHandler ClassA.Event3, AddressOf ClassX.Event3Handler
RemoveHandler ClassA.Event4, AddressOf ClassX.Event4Handler

This is not so bad for a small number of classes and events, but I

actually
have about 30 classes and 50 events. The combinations are too large to
contemplate the longhand approach.

.NET must keep track of what event is linked to what handler somewhere so that it knows what to call. I am looking for a generic (iterative?) way to detach handlers from events, given only the source class and target class. These classes implement interfaces that contain the events and handlers if that helps.

Can anyone give me any clues?

TIA

Charles


Nov 20 '05 #5
Well, almost 100% ...

It's tantalisingly close, but there is still one slight gap.

The article shows how to find methods on a receiver that match the pattern
OnXXXX given the sender. It loops through the sender events and tries to get
methods from the receiver that match the pattern. For each one it finds it
either adds or removes a handler for the event.

This is great where there is a straight forward mapping, but I don't have a
straight forward mapping. For example, although I could easily use this
pattern in many cases, there are situations where the sender has two similar
events, only one of which a particular receiver is going to service.

Let's say that my sender has an event ValueChanged. My receiver could easily
have a method OnValueChanged that could be linked up using the technique
above. As it happens, my receiver has a method MyControl_ValueChanged, but
this also works with the technique above.

The problem arises where my sender has two values that can change. In this
case it can raise two events: Value1Changed and Value2Changed. My receiver
only services one of these, and which one is determined at runtime.
Therefore, the receiver still has a method MyControl_ValueChanged, because
it knows nothing of a sender that has two values.

This breaks the pattern, as there is now easy way to transform the event
Value2Changed into the method name MyControl_ValueChanged.

So, ...

I am trying to find a way of looping through the sender's events, as before,
but for each one I want to get the invocation list, and see if my receiver
is in it. Only if it is do I want to remove the handler. I have got this far

<code>
Public Shared Sub RemoveHandlers(ByVal sender As Object, ByVal receiver
As Object)

Dim SenderType As Type
Dim ReceiverType As Type

Dim dlgts() As [Delegate]

SenderType = sender.GetType()
ReceiverType = receiver.GetType()

For Each ei As EventInfo In SenderType.GetEvents()

' For each event that the sender can raise, get the invocation
list
dlgts = sender.Events.Item( *WHAT GOES
HERE?* ).GetInvocationList()

For Each dlgt As [Delegate] In dlgts
If *TEST FOR DELEGATE BEING ON RECEIVER?* Then
ei.RemoveEventHandler(sender, dlgt)
End If
Next dlgt

Next ei

End Sub
</code>

Can you suggest what might replace the capitalised text? Alternatively, can
you think of another wau of achieving the same end?

Thanks.

Charles
[If I can crack this then I will have only two problems remaining, one of
which is a totally random crash, that can occur at any time, at any place,
and cause immediate termination without going through any exception
handlers. I am using the Infragistics UltraWinToolbars, et al]
"yEaH rIgHt" <nospam@haha> wrote in message
news:10*************@corp.supernews.com...
Take a look at this, it might help.

http://www.vbinfozine.com/t_bindevt.shtml


"Charles Law" <bl***@nowhere.com> wrote in message
news:O7**************@TK2MSFTNGP09.phx.gbl...
I think I asked the wrong question last time, so I am starting a separate post to distinguish them.

Take five classes: ClassA and ClassW...Z

ClassA raises five events: Event1...5

ClassW...Z handle some combination of these events. For example

ClassW handles Event1, 2 and 5
ClassX handles Event1, 3 and 4
ClassY handles Event2 and 4
...

At some point, I want to detach ClassX from ClassA, so if I were to write the code longhand, I would have

RemoveHandler ClassA.Event1, AddressOf ClassX.Event1Handler
RemoveHandler ClassA.Event3, AddressOf ClassX.Event3Handler
RemoveHandler ClassA.Event4, AddressOf ClassX.Event4Handler

This is not so bad for a small number of classes and events, but I

actually
have about 30 classes and 50 events. The combinations are too large to
contemplate the longhand approach.

.NET must keep track of what event is linked to what handler somewhere so that it knows what to call. I am looking for a generic (iterative?) way to detach handlers from events, given only the source class and target class. These classes implement interfaces that contain the events and handlers if that helps.

Can anyone give me any clues?

TIA

Charles


Nov 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Csaba Gabor | last post by:
Is there a way to remove an anonymous event handler that I attached using attachEventListener? Or to say it another way, is there a way to remove all event handlers that have been attached to an...
1
by: MuZZy | last post by:
Hi, Is there a way to remove all event handlers for a control's event? Say, i have a button and i want to remove all button.Click events for it - i don't know how many of them was hooked to the...
23
by: Peter Row | last post by:
Hi, I am currently working on a VB.NET project that has been going for quite a while. In the past it has been developed with a lot of compatibility VB6 functions and constants, e.g Left(),...
6
by: Peter Michaux | last post by:
Hi, Douglas Crockford mentioned in a video on Yahoo! that before removing an element it is a good idea to purge it's event handlers. I think he was only refering to the event handlers defined...
11
by: MikeT | last post by:
This may sound very elementary, but can you trap when your object is set to null within the object? I have created a class that registers an event from an object passed in the constructor. When...
5
by: gnassar | last post by:
Essentially my problem is that .NET 2005 is removing my event handlers. There's no real special things about my project, it just continually removes them all. It starts on the open of a...
3
by: Gary Stephenson | last post by:
Can someone please explain why the assertion in the following code fails? Could the assertion be made to succeed somehow? // code begins class MyButton : Button { public MyButton() { Click...
4
by: m0nkeymafia | last post by:
I read about this technique to basically allow you to have a file with behaviours and javascript events that attach to various dom objects and events but are not done inline. so its basically a...
0
by: khalid galal | last post by:
Hi, i am having a problem with removing event handlers, it is when creating nested event handlers (an event handler raising another event handler) where a part of the code is private void...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.