Results 1 to 8 of 8

Thread: [RESOLVED] Disposing a global Font variable - keeping my code clean ;)

  1. #1

    Thread Starter
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    Resolved [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:
    1. Option Strict On
    2. Public Class frmA
    3.    Dim Font1 As System.Drawing.Font  
    4.    Dim Font2 As System.Drawing.Font  
    5.    Dim Font3 As System.Drawing.Font
    6.  
    7.    Private Sub frmA_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    8.       'Do something on load
    9.       Start()   'cals method Start that will call all other methods...
    10.    End Sub
    11.  
    12.    Private Sub Start()
    13.     Try
    14.       Font1 = New Font("Arial", 10, FontStyle.Regular)
    15.       Font2 = New Font(Font1.Name, 12, FontStyle.Underline)
    16.       Font3 = New Font(Font1.Name, 14, FontStyle.Bold)
    17.       One()
    18.       Two()
    19.     Finally   '==>> this is how disposing is now done: in Finally...
    20.       Font1.Dispose()
    21.       Font2.Dispose()
    22.       Font3.Dispose()
    23.     End Try
    24.    End Sub
    25.  
    26.    Private Sub One()
    27.       'draw something with above fonts...
    28.    End Sub
    29.  
    30.    Private Sub Two()
    31.       'draw something with above fonts...
    32.    End Sub
    33. End Class
    Thank for any help...
    (.NET Framework 2, VS 2008, Win XP)
    1. If this post helped you, please Rate it = That's You, saying Thanks, to Me ...Left side of this post: [Rate this post]
    2. Mark this Thread Resolved if your question has been answered That's You, saying Thanks, to Group ...Menu on top of your original Post: [Thread Tools]>[Mark Thread Resolved]
    3.
    Check my site: www.er-ef.netCheck my snippets: Get installed .NET versionsRegex extractingJoin hierarchically nested Datatables in one flattened Datatable


  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    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.
    1. If this post helped you, please Rate it = That's You, saying Thanks, to Me ...Left side of this post: [Rate this post]
    2. Mark this Thread Resolved if your question has been answered That's You, saying Thanks, to Group ...Menu on top of your original Post: [Thread Tools]>[Mark Thread Resolved]
    3.
    Check my site: www.er-ef.netCheck my snippets: Get installed .NET versionsRegex extractingJoin hierarchically nested Datatables in one flattened Datatable


  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Disposing a global Font variable - keeping my code clean ;)

    Quote Originally Posted by Zeljko View Post
    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.

    Quote Originally Posted by Zeljko View Post
    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.

    Quote Originally Posted by Zeljko View Post
    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    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."

  6. #6

    Thread Starter
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    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...
    1. If this post helped you, please Rate it = That's You, saying Thanks, to Me ...Left side of this post: [Rate this post]
    2. Mark this Thread Resolved if your question has been answered That's You, saying Thanks, to Group ...Menu on top of your original Post: [Thread Tools]>[Mark Thread Resolved]
    3.
    Check my site: www.er-ef.netCheck my snippets: Get installed .NET versionsRegex extractingJoin hierarchically nested Datatables in one flattened Datatable


  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Private Sub Start()
    2.     Dim f1 As New Font(FontFamily.GenericSansSerif, 10)
    3.     Dim f2 As New Font(FontFamily.GenericSerif, 12)
    4.  
    5.     Me.One(f1)
    6.     Me.Two(f2)
    7.  
    8.     f1.Dispose()
    9.     f2.Dispose()
    10. End Sub
    11.  
    12. Private Sub One(ByVal f As Font)
    13.     'Use f here.
    14. End Sub
    15.  
    16. Private Sub Two(ByVal f As Font)
    17.     'Use f here.
    18. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    Re: [RESOLVED] Disposing a global Font variable - keeping my code clean ;)

    Quote Originally Posted by jmcilhinney View Post
    You should do as tg said
    I know and I will

    Quote Originally Posted by jmcilhinney View Post
    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

    Quote Originally Posted by jmcilhinney View Post
    You're just trying to read too much into something that is very simple.
    You got that right in the middle!
    1. If this post helped you, please Rate it = That's You, saying Thanks, to Me ...Left side of this post: [Rate this post]
    2. Mark this Thread Resolved if your question has been answered That's You, saying Thanks, to Group ...Menu on top of your original Post: [Thread Tools]>[Mark Thread Resolved]
    3.
    Check my site: www.er-ef.netCheck my snippets: Get installed .NET versionsRegex extractingJoin hierarchically nested Datatables in one flattened Datatable


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width