Anyone who’s done any UI work with .Net and WinForms is very familiar with this type of code:
TestForm frm = new TestForm();
frm.ShowDialog();
I found myself wishing that a call to show a modal dialog was a little less verbose, more like a static call. Andf there is a simpler way. All you really need, in a simple case, is something like this:
new TestForm().ShowDialog();
Am i missing anything? Could there be any repercussions from this kind of shorthand? E.g. windows messages not handled/routed correctly, dialog’s resources not disposed etc.?
Was working on this when i saw Ray’s feedback:
i suppose an even shorter way would be to create a static member withing TestForm that creates an instance of itself and calls ShowDialog internally. So, this code:
public static DialogResult DoModal()
{
return new TestForm().ShowDialog();
}
could be invoked thusly:
TestForm.DoModal();
If you don’t want to reuse the form object anywhere in your code you can just use the short form
If you want to do something later with that object then you must assign it to a variable.