0

I have declared few WithEvent handlerr like

Private WithEvents mErrorList As ErrorList

Private WithEvents mControlManager As ControlManager

I used this handler to subscribe the events in a class such as

    Private Sub mControlManager_PageLoaded(sender As Object, e As DataEntry.PageLoadEventArgs) Handles mControlManager.PageLoaded
      // line of code
    End Sub

Similar way....

        Private Sub mErrorList_ErrorSelected(ByVal sender As Object, ByVal e As ErrorSelectedEventArgs) Handles mErrorList.ErrorSelected
       // line of code
       End Sub

but in dispose method

 Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then                   
             Me.mErrorList = Nothing    
             Me.mControlManager = Nothing
           End If
    End If
    Me.disposedValue = True
   End Sub       

In the dispose method I set the objects as equal to Nothing. So my question do I need to remove above the Handler in Dispose() method or object = Nothing is sufficient. in such a way

 Protected Overridable Sub Dispose(ByVal disposing As Boolean)
     If Not Me.disposedValue Then
       If disposing Then
        RemoveHandler Me.mErrorList.ErrorSelected, AddressOf Me.mErrorList_ErrorSelected                
        Me.mErrorList = Nothing
        RemoveHandler Me.mControlManager.PageLoaded, AddressOf mControlManager_PageLoaded    
        Me.mControlManager = Nothing
      End If
    End If
    Me.disposedValue = True
   End Sub
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
Pritish
  • 2,194
  • 2
  • 21
  • 44
  • None of this code is correct. Use the Dispose method only to call the Dispose method of any fields in your class, if they have one. – Hans Passant Mar 21 '16 at 08:16
  • @HansPassant I have just add code for understanding to the other SO user. my question is do I used RemoveHandler In Dispose() or not. while I am setting a values to nothing of class object(variable). – Pritish Mar 21 '16 at 08:47

1 Answers1

2

There are a few questions in here, so I will paraphrase and answer one by one.

Do I need to Call RemoveHandler when creating object declared WithEvents

No

Do I need to Dispose of objects in the Dispose method

If any objects you are using implement IDisposable and you are not wrapping then in a Using block then your class should also implement IDisposable and call Dispose on each of these objects

More Info: Using block vs IDisposabe.Dispose()

Do I need to set each object = Nothing in the Dispose method

No

More info: Set variables to "Nothing" is a good practice?

Community
  • 1
  • 1
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143