|
-
Aug 13th, 2010, 11:14 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Disposing a global Font variable - keeping my code clean ;)
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'?
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
Thank for any help...
(.NET Framework 2, VS 2008, Win XP)
-
Aug 13th, 2010, 12:18 PM
#2
Re: Disposing a global Font variable - keeping my code clean ;)
Don't declare your font variables at the class level... there's no need for that ... scope them to the start sub... if you need them in the One & Two subs... pass the ones you need in... there really isn't a need for class level variables that are only used in a short portion of the code for a limited time.
-tg
-
Aug 13th, 2010, 01:01 PM
#3
Thread Starter
Hyperactive Member
Re: Disposing a global Font variable - keeping my code clean ;)
How can I declare a Font in Start sub and then use it in other methods (one, two...)?
tg:"there really isn't a need for class level variables that are only used in a short portion of the code for a limited time"
Methods One, Two...etc are not short by any means. That is here only to show my situation in short 
I have approximately 10 methods One,Two, Three...Ten and each method having ~1000+ lines of code. In time they will definitely expand so I need a system where I can control all of my fonts from one place, withe easiness.
tg:"there really isn't a need for class level variables that are only used in a short portion of the code for a limited time"
This methods (One,Two...) are drawing to one surface and they are constantly refreshed (by invalidating) to redraw surface when conditions (calculations) changes. So it can be short time but usually is longer time.
-
Aug 13th, 2010, 01:37 PM
#4
Re: Disposing a global Font variable - keeping my code clean ;)
 Originally Posted by Zeljko
How can I declare a Font in Start sub and then use it in other methods (one, two...)?
What did I say? "pass the ones you need in" .. pass them in using parameters.
 Originally Posted by Zeljko
tg:"there really isn't a need for class level variables that are only used in a short portion of the code for a limited time"
Methods One, Two...etc are not short by any means. That is here only to show my situation in short 
I have approximately 10 methods One,Two, Three...Ten and each method having ~1000+ lines of code. In time they will definitely expand so I need a system where I can control all of my fonts from one place, withe easiness.
I don't mean short as in lines of code... I meant it in terms of the application as as whole... the only time you're using them is when the Start sub runs... the fact that Start calls One and Two and they are thousands of lines long is irrelevant... it's still withing the confines of the Start sub.
 Originally Posted by Zeljko
tg:"there really isn't a need for class level variables that are only used in a short portion of the code for a limited time"
This methods (One,Two...) are drawing to one surface and they are constantly refreshed (by invalidating) to redraw surface when conditions (calculations) changes. So it can be short time but usually is longer time.
Again, I said LIMITED time... not short.... not the same thing... see my previous comment.
The other solution is to not create your font in Start.... but do it as part of their declaration. Since they don't really change, I don't see a reason to do it any other way.
Code:
Dim Font1 As System.Drawing.Font = New Font("Arial", 10, FontStyle.Regular)
Dim Font2 As System.Drawing.Font = New Font(Font1.Name, 12, FontStyle.Underline)
Dim Font3 As System.Drawing.Font = New Font(Font1.Name, 14, FontStyle.Bold)
Change Start:
Code:
One()
Two()
'There's no point now for a Try ... since we're not going to use the Finally and you aren't Catching any exceptions...
Now in the Disposed event...
Code:
Private Sub FrmA_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
Font1.Dispose()
Font2.Dispose()
Font3.Dispose()
End Sub
-tg
-
Aug 13th, 2010, 02:11 PM
#5
Re: Disposing a global Font variable - keeping my code clean ;)
You need to implement the IDisposable method for your class.
When you press the enter key after typing Implements IDisposable, several routines will be added (two). Dispose of your fonts in the dispose method.
Of course, every object which contains this object will need to call the dispose method.
Edit: eh, it looks like your 'object' is a form. If these are global fonts then you could create a shared class (or module) which creates the fonts at application startup and destroys/disposes them at shutdown.
Last edited by SJWhiteley; Aug 13th, 2010 at 02:15 PM.
"Ok, my response to that is pending a Google search" - Bucky Katt.
"There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
"Before you can 'think outside the box' you need to understand where the box is."
-
Aug 14th, 2010, 11:40 PM
#6
Thread Starter
Hyperactive Member
Re: Disposing a global Font variable - keeping my code clean ;)
After reading all suggestions, I think I will stick with current version with minor changes:
- assign fonts in frmA_Load event
- dispose fonts in frmA_Closing event
Fonts are all in one place for easy control, I hope they all correctly disposed, FxCop still complains, so it is still not what I wanna achieve but hey, I can get always clearer code.
Thanks all for helping...
-
Aug 15th, 2010, 12:54 AM
#7
Re: [RESOLVED] Disposing a global Font variable - keeping my code clean ;)
Putting everything in one place is bad design. Everything should exist ONLY where it's needed. You should do as tg said. Really, how can someone with 360 posts have to ask how to declare a variable and pass an argument to a method? You know what to do because you have already declared variables and passed arguments before. You're just trying to read too much into something that is very simple. Just do EXACTLY what was posted, e.g.
vb.net Code:
Private Sub Start() Dim f1 As New Font(FontFamily.GenericSansSerif, 10) Dim f2 As New Font(FontFamily.GenericSerif, 12) Me.One(f1) Me.Two(f2) f1.Dispose() f2.Dispose() End Sub Private Sub One(ByVal f As Font) 'Use f here. End Sub Private Sub Two(ByVal f As Font) 'Use f here. End Sub
-
Aug 15th, 2010, 12:21 PM
#8
Thread Starter
Hyperactive Member
Re: [RESOLVED] Disposing a global Font variable - keeping my code clean ;)
 Originally Posted by jmcilhinney
You should do as tg said
I know and I will 
 Originally Posted by jmcilhinney
Really, how can someone with 360 posts have to ask how to declare a variable and pass an argument to a method?
I was (over)thinking it 
 Originally Posted by jmcilhinney
You're just trying to read too much into something that is very simple.
You got that right in the middle!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|