1

Is line 3 of this method is unnecessary?

Private Sub KillLabel(ByVal lbl As Label)
    If Not (IsNothing(lbl)) Then
        RemoveHandler lbl.Click, AddressOf GantEvents_Click
        lbl.Dispose()
    End If
End Sub
  • Instead of `If Not (IsNothing(lbl)) Then` you should write `If lbl IsNot Nothing Then`. Here's why: https://stackoverflow.com/a/24494417 – Visual Vincent Dec 03 '17 at 15:25
  • Also: Possible duplicate of [Where to "destroy" Handlers in VB.NET classes?](https://stackoverflow.com/questions/4827237/where-to-destroy-handlers-in-vb-net-classes) -- The question and answer is about forms and container controls, but the same applies to all controls. When a control is disposed its event handlers are automatically removed. – Visual Vincent Dec 03 '17 at 15:26
  • 4
    Not necessary. Control.Dispose() does not tinker with event subscriptions. Calling Dispose() is only valid when your program has no remaining references left to the Label object. But you do, you passed it as an argument to this method. What we can't see is where it is stored. If you got it from the Controls collection then nothing needs to be done, the Dispose() call removed it from that collection. If it is, say, stored in a member variable of the Form class (Me.Label1 for example) then you would have to set that variable to Nothing to allow the label object to be collected. – Hans Passant Dec 03 '17 at 15:46
  • What @HansPassant said... – Trevor Dec 03 '17 at 16:27
  • @HansPassant thank you, I see that there is a claim that this question is a duplicate of other question, before I have asked that question I have tried a lot to search answer for that question with no results...now I see why...Where to “destroy” Handlers in VB.NET classes<> Where to “DISPOSE” Handlers in VB.NET classes... –  Dec 03 '17 at 17:46
  • @VisualVincent tnx for the tip I will use that. –  Dec 03 '17 at 17:46
  • I'm not sure whether you meant your question is a duplicate or not (I was the one to flag it as a duplicate btw), but I found it by simply googling _"Does dispose remove event handler VB"_. – Visual Vincent Dec 03 '17 at 17:55

0 Answers0