0

Let's say i do like this twice:

AddHandler grid.SelectedIndexChanged, AddressOf doSomething
AddHandler grid.SelectedIndexChanged, AddressOf doSomething

From my investigation it will be subscribed and fire up every time twice. Means i can subscribe same handler as much times as i want.

However does it mean when i do somewhere only once like this:

RemoveHandler grid.SelectedIndexChanged, AddressOf doSomething
  1. Would it mean i have still one "same subscriber" right therefore according to above sample handler will be called once after removing just one time?
  2. It goes to the fact that i have to always remove handler as much as i add it?
  3. What about when i execute more removehandler command for specific event than i added it?
  4. Is there any way to remove all subscribers from particular event? I am asking because

Explanation: I have huge project developed by previous developer where he always doing addhandler, remove handler from lot different places... I recognized that sometimes even if at particular time shouldn't be any of subscribers left (his removehandler..) they are even run couple times same handlers are called ! The problem is the form contains "long live objects" and i am struggling with that project right now. Hope getting some help from you.

Thanks !

braX
  • 11,506
  • 5
  • 20
  • 33
Unknown
  • 39
  • 10
  • Your Questions point 1 to 3 is stuff you can test by yourself. Point four is something you could simply google by yourself and get, for example, [this](https://stackoverflow.com/questions/1150250/removing-all-event-handlers-in-one-go) – Detonar Nov 21 '17 at 12:59
  • Whilst the comment above maybe obvious I have exactly the same questions and this question has now first brought me to the SO website and also assisted me with the questions. – darbid Jan 31 '19 at 08:16

1 Answers1

0

You just need to try it. Two event will be called and one event will be removed at a time.

Module Module1

    Delegate Sub TestDelegate()

    Event TestEvent As TestDelegate

    Sub Main()

        AddHandler TestEvent, AddressOf TestFunc
        AddHandler TestEvent, AddressOf TestFunc

        Console.WriteLine("---")
        RaiseEvent TestEvent() ' 'a' writted twice

        RemoveHandler TestEvent, AddressOf TestFunc

        Console.WriteLine("---")
        RaiseEvent TestEvent() ' 'a' written once

        RemoveHandler TestEvent, AddressOf TestFunc

        Console.WriteLine("---")
        RaiseEvent TestEvent() ' 'a' not written

        RemoveHandler TestEvent, AddressOf TestFunc ' No error hapenning

        Console.ReadLine()

    End Sub

    Public Sub TestFunc()

        Console.WriteLine("a")

    End Sub

End Module

To remove all event, check these questions.

the_lotus
  • 12,668
  • 3
  • 36
  • 53