CA2000 : Microsoft.Reliability : In method ‘xxx’, call System.IDisposable.Dispose on object ‘XXX’

When I try to compile my .Net application I have received the warning below:
“CA2000 : Microsoft.Reliability : In method ‘GetResponse’, call System.IDisposable.Dispose on object ‘XXX’ before all references to it are out of scope.

When I look up this warning on Microsoft MSDN, it said, it is caused by “A local object of a System.IDisposable  type is created but the object is not disposed before all references to the object are out of scope.

Althought it is just a warning, I guess it would could improve the efficient/performance a bit, if I do what it said. At the end, I used the “Using Statement” [in VB.net] to wrap objects inside that method, and the warning has gone.

Here it is my sample code:

Public Function GetResponse as ShareObject
Dim MyHelperInfo As New MyHelperClass

Dim response As ShareResponse
Using MyHelperInfo
Dim WebServiceShareChiWai As New WebServiceShareChiWaiRequest
WebServiceShareChiWai.Param1 = “SharechiwaiVariable”
response = MyHelperInfo.SendRequest(WebServiceShareChiWai)
End Using
Return response.ShareObject

End Function

Hope you find it useful.

Happy coding =)

Leave a Reply