I have a class that implements IDisposable because it has a private member field "foo" that is IDisposable (which is initialized in the constructor). I am unexpectedly getting a CA2000 Code Analysis error, which wants me to be sure to dispose of foo. However, I have foo.Dispose() in my class's Dispose() code, which should take care of this.
I did some searching around and surprisingly can't find an answer. What am I doing wrong? Clearly I missing something basic. How do I write my code to overcome this?
My VB code:
Public Class Bar
Implements IDisposable
Private Foo As SomeDisposableThing
Public Sub New()
Foo = New SomeDisposableThing() With {.name = "hello"}
End Sub
'''' snip ''''
Private disposedValue As Boolean = False ' To detect redundant calls '
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
If Foo IsNot Nothing Then Foo.Dispose()
End If
End If
Me.disposedValue = True
End Sub
Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
End Class