Questions about Dispose()

  • Thread starter Thread starter TheSteph
  • Start date Start date
T

TheSteph

Hi,

In the example below, do I have to call Dispose for the 2 ImageList ?
Do I have to do it in the Form.Disposed event ?


public class FRM_NavBrowseConfig : Form
{
//...
private ImageList FSmallImageList = new ImageList();
private ImageList FLargeImageList = new ImageList();
//...

// *** IS THIS CORRECT/NECESSARY ? ***
public FRM_NavBrowseConfig()
{
this.Disposed += new EventHandler(FRM_NavBrowseConfig _Disposed);
}

static void FRM_NavBrowseConfig _Disposed(object sender, EventArgs e)
{
FSmallImageList.Dispose()
FLargeImageList.Dispose()
}
// *** END ***

}//EndClass FRM_NavBrowseConfig



Thanks for any advices !!

Steph.
 
TheSteph,

If you have dropped the listview component from the toolbar you don't need
to do anything because the designer creates the image list using the
contructor that accepts IContainer interface. Doing this the image list goes
in the components collection, which elements are disposed upon form
disposal.

BTW if you want to dispose some objects you don't need to create event
handler, The Dispose method is already overriden for you (when VSS wizard
creates the form class) and you can put your code there.
 
Thanks for your reply but I haven't dropped the ImageList from the
toolbar... So they are not in the designer generated code in any way... And
I red that after use, one should call the Dispose() Meth. of any components
that have one.... That's why I ask my question...
 
TheSteph,

You don't have to call Dispose, but it's good practice. If you don't call it
the object will be disposed upon finalization.
 
Back
Top