Questions about Dispose()

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

Stoitcho Goutsev \(100\)

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

TheSteph

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

Stoitcho Goutsev \(100\)

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top