Re: Dispose

kara hewett <[email protected]> Thu, 12 Jul 2007 07:07:24 -0400
Newsgroups gmane.comp.windows.devel.dotnet.web
Message-ID <[email protected]>
Hi Andrew,

The PatientPhoto class should implement IDisposable like

public class PatientPhoto: IDisposable
{
~PatientPhoto()
{
   Dispose();
}

}

and dispose of the Photo object within the Dispose implementation.  After
the Photo object is disposed, it seems unnecessary to set the object to
null.  Optionally you could suppress further garbage collection within the
Dispose implementation through GC.SuppressFinalize(this);

Kara


On 7/11/07, Andrew Dunn <[email protected]> wrote:
>
> Hi,
>
> Can anyone see a better way of doing this. I have a class that holds an
> image as one of it's properties. This image is held in the windows temp
> directory. I want to delete it when the class is destroyed. Is Dispose
> the best place to do this? Is ~PatientPhoto() OK to call dispose?
>
>
>
>
> public Bitmap Photo
> {
>    get{ return _Photo;}
>    set{ _Photo = value;}
> }
>
> ~PatientPhoto()
> {
>    Dispose();
> }
>
> public void Dispose()
>        {
>            if(Photo != null)
>            {
>                if (Photo.Tag != null)
>                {
>                    string FileLoc = Photo.Tag.ToString();
>
>                    ///Delete the photo file as it is stored in a
> temporary file in the windows temp directory
>                    if (File.Exists(FileLoc))
>                    {
>                        Photo.Dispose();
>                        Photo = null;
>
>                        try
>                        {
>                            File.Delete(FileLoc);
>                        }
>                        catch (IOException e)
>                        {
>
> System.Windows.Forms.MessageBox.Show(e.Message);
>                        }
>                    }
>                }
>            }
>
>        }
>
> ===================================
> This list is hosted by DevelopMentor(r)  http://www.develop.com
>
> View archives and manage your subscription(s) at
> http://discuss.develop.com
>

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com