Click here to Skip to main content
15,891,763 members
Articles / Programming Languages / Visual Basic

Simplified Resource Management with the DisposableCollection Class

Rate me:
Please Sign up or sign in to vote.
4.42/5 (8 votes)
26 Sep 20044 min read 55.3K   367   17  
This article demonstrates how a strongly typed collection of IDisposable objects can be used to simplify the management of multiple resources in .NET.
'////////////////////////////////////////////////////////////////////////////////////
'// Form1.cs
'//
'// By Scott McMaster (smcmaste@hotmail.com)
'// 09/25/2004
'//
'// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
'// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
'// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
'////////////////////////////////////////////////////////////////////////////////////
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call
        SetStyle(ControlStyles.ResizeRedraw, True)
    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        components = New System.ComponentModel.Container
        Me.Text = "Form1"
    End Sub

#End Region

    '// Do some painting using DisposableCollection to manage our GDI+ instances.
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)
        Dim g As Graphics = e.Graphics

        Dim dc As New DisposableCollection
		
        '// When the Finally block executes, the DisposableCollection cleans up
        '// all instances that have been added to it.
        Try
            Dim ellipseBrush As New SolidBrush(Color.Red)
            dc.Add(ellipseBrush)

            Dim textFont As New Font("Arial", 16)
            dc.Add(textFont)

            Dim textBrush As New SolidBrush(Color.Black)
            dc.Add(textBrush)

            g.FillEllipse(ellipseBrush, ClientRectangle)
            g.DrawString("Hello World", textFont, textBrush, 50.0F, 50.0F)
        Finally
            dc.Dispose()
        End Try

    End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I have over 10 years of full-lifecycle software development experience on a variety of large projects. I have a B.S. in Math from the University of Nebraska-Lincoln, a Masters in Software Engineering from Seattle University, and a Masters in Computer Science from the University of Maryland. I specialize in building and installing tools, frameworks, and processes that improve developer productivity and product quality. I am currently based in the Seattle area.

Comments and Discussions