tags:

views:

41

answers:

2
private void btnMail_Click(object sender, EventArgs e)
{
    new formCustomerReportMailer().Show();
}

Is it ill-advised to create a form without assigning it to variable? I don't plan to use it otherwise and it seems that forcefully calling the garbage collector to collect unused references doesn't seem to close the form. Any downside to this or future problems?

+1  A: 

Yes, you should dispose the form. It isn't automatic when you use ShowDialog(). Why is this a dialog in the first place when you are not interested in the results?


After edit: this is not a problem. The exact same thing happens in Program.cs

Hans Passant
Whoops, it should be Show(), Thanks for pointing that out!
MikeAbyss
Hmm, rather a big whoops. No, with Show() everything is fine.
Hans Passant
+3  A: 

Use the following code template:

using (Form myForm = new Form())
{
   myForm.ShowDialog();
}

This will display and dispose the form as necessary.

Bernard