I have declared Fonts at class level to use it through my methods in that form. Form 'frmA' is shown with ShowDialog and when it closes or stops using fonts I'd like to properly dispose fonts.
When I run code analysis FxCop it complaints with:
CA2213 : Microsoft.Usage : 'frmA' contains field 'frmA.Font1' that is of IDisposable type: 'Font'. Change the Dispose method on 'frmA' to call Dispose or Close on this field...
How to properly dispose globally declared fonts so that is "by the rules" and FxCop does not complain any more?
How to 'Change the Dispose method on 'frmA' to call Dispose'?
Thank for any help...VB.Net Code:
Option Strict On Public Class frmA Dim Font1 As System.Drawing.Font Dim Font2 As System.Drawing.Font Dim Font3 As System.Drawing.Font Private Sub frmA_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Do something on load Start() 'cals method Start that will call all other methods... End Sub Private Sub Start() Try Font1 = New Font("Arial", 10, FontStyle.Regular) Font2 = New Font(Font1.Name, 12, FontStyle.Underline) Font3 = New Font(Font1.Name, 14, FontStyle.Bold) One() Two() Finally '==>> this is how disposing is now done: in Finally... Font1.Dispose() Font2.Dispose() Font3.Dispose() End Try End Sub Private Sub One() 'draw something with above fonts... End Sub Private Sub Two() 'draw something with above fonts... End Sub End Class
(.NET Framework 2, VS 2008, Win XP)





Reply With Quote