Click here to Skip to main content
15,889,635 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have the following piece of code, just for reference, and im facing the problem that upon running the code Analysis tool i get the error CA200

CA2000 : Microsoft.Reliability : In method , call System.IDisposable.Dispose on object 'newCol' before all references to it are out of scope.

VB
Dim newCol As New ColumnHeader
newCol.TextAlign = 'whatever value
newCol.Text = 'whatever value
newCol.Tag = 'whatever value
newCol.Width = 'whatever value

ListView1.Columns.Add(newCol)


And i was wondering to to deal with this, or similar situations, that i can not use newcol.Dispose() after adding it to the ListView, for obvious reasons, nor i can use the using statements.

What is the best code practice for this? what I'm missing?
Posted

1 solution

The important thing to understand is that these warnings are, just that, warnings. They don't cater for all cases, and can be too broad brush at times because they are looking in your code for the case to be handled, not something that may be 30 levels of indirection away. In this case, disposal of the listview should internally manage the disposal of all resources allocated by the listview framework.

If I were you, I'd probably suppress this message with the SuppressMessage attribute.
 
Share this answer
 
Comments
creizlein 10-May-11 19:09pm    
Thanks, that sounds like a fair explanation, i just want to make sure that there is nothing from on the code or some other kinda of disposing tip that i wasn't implementing correctly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900