views:

43

answers:

1

I use to set WithEvents variables to Nothing in Destuctor, because this will "Remove" all the Handlers associated with Handles keyword. Will this have the same effect for derivated classes?

Class A
    Protected WithEvents _Foo as Button

    Private Sub _Foo_Click Handles _Foo.Click
        ' ... some Click action '
    End Sub

    Public Sub Dispose(disposing as Boolean)
        If disposing then _Foo = Nothing ' remove handler _Foo_Click '
    End Sub
End Class

Class B
    Inherits A

    Private Sub _Foo_Move Handles _Foo.Move
        ' ... some Move action '
    End Sub

    ' ????? will or NOT remove OR handler _Foo_Move the base Dispose??'
    Public Overrides Sub Dispose(disposing as Boolean)           
        'If disposing then _Foo = Nothing '
        MyBase.Dispose(disposing)
    End Sub
End Class

PS. For more clarity, suppose that _Foo button comes from exterior and lives more that the A class.

+1  A: 

Yes. The _Foo = Nothing statement in the derivated class will change the base class' protected local _Foo to nothing, thus removing all event handlers.

M.A. Hanin