Resolved Inherit ListView, Dispose

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,530
Location
Lansing, MI; USA
Programming Experience
10+
I have a user control that inherits the FW ListView and I need to dispose of two objects when the form's disposing of it's objects.

Here's what I've concluded already, am I on the right track?
VB.NET:
    Public Shadows Sub Dispose()
        'Dispose of my object here...
        MyBase.Dispose()
    End Sub
 
No, override the existing Dispose(Boolean) method instead. When you Shadow that method is only called when object is cast as that type or inherited types. In most cases Dispose is called when object is cast as IDisposable or base types. What happens when you override is that when base Dispose method is called that method calls your override. These explanations may seem confusing, try both ways and make the call below and you'll see what I mean, add a Messagebox or something in to see if your Dispose is called or not.
VB.NET:
Dim fw As New FWListview
CType(fw, IDisposable).Dispose()
With the Dispose(Boolean) override, try this below, again notice if your Dispose method is called:
VB.NET:
fw.Dispose()
I also recommend you read the help topics about Shadows and Overrides.

Another option (to override) is to handle the Disposing event, but normally you override when deriving classes.
 
What I would like to do is override it since it's an inherited control, but VS tells me to replace 'Overrides' with 'Shadows' still so it's either shadow it or handle it's event and call it on the base still. Here's my code:
VB.NET:
Public Class MoveItemListView
    Inherits System.Windows.Forms.ListView

    Private m_InserLineBr As SolidBrush
    Private m_InserLinePn As Pen

    Public Shadows Sub Dispose()
        m_InserLineBr.Dispose()
        m_InserLinePn.Dispose()
        MyBase.Dispose()
    End Sub

    Public Shadows Sub Dispose(ByVal Disposing As Boolean)
        m_InserLineBr.Dispose()
        m_InserLinePn.Dispose()
        MyBase.Dispose(Disposing)
    End Sub

End Class
Stuff like the constructor and everything else isn't included in the above code.
 
Shadows really can mess things up. This is the one:
VB.NET:
Public Class MoveItemListView
    Inherits System.Windows.Forms.ListView

    Private m_InserLineBr As SolidBrush
    Private m_InserLinePn As Pen

    Protected Overrides Sub Dispose(ByVal Disposing As Boolean)
        m_InserLineBr.Dispose()
        m_InserLinePn.Dispose()
        MyBase.Dispose(Disposing)
    End Sub

End Class
The reason you couldn't add it as normal using the Overrides list was because your Shadows hid it. I should perhaps have been more clear when I suggested you try both methods to see how inheritance works that you could only do one at a time.
 
I didn't get a chance to post again last night but I did end up getting a sub like that to work.

Also when I was playing with "both" methods, I had the shadows subs commented out so I'd know that they wouldn't interfere.
 
Back
Top