In my WPF application, I have a few places that based on a given ID of a record, I call a new form to be displayed as modal to view details. It then closes and returns back to calling source as expected. All this works no problem.
To keep this simplified in coding, I put a "ShowDialog()" call at the end of the constructor of the form being displayed. This prevents the need of every place that this form being called requires something like..
var myModalForm = new MyModalForm(someIdToDisplay);
myModalForm.ShowDialog();
Simplified, I just need to create the modal form with the Id, such as
new MyModalForm(someIdToDisplay);
But through the ReSharper inspector, it comes back with "Possible unassigned object created by 'new' expression".
I know the garbage collector will get it when it's finished, but being a modal form, once it's done, I don't need to do anything else with it. So, is this bad, or ok and just ignore this type of warning consideration. Everything else works fine in the application otherwise.
To keep this simplified in coding, I put a "ShowDialog()" call at the end of the constructor of the form being displayed.
That sounds like an ugly design to me, personally. Constructors are designed to return a usable object - and ideally that's all they should do.
I would change this to a static method in MyModalForm
:
public static void ShowForId(int id)
{
var form = new MyModalForm(id);
form.ShowDialog();
}
Then your calling code can just be:
MyModalForm.ShowForId(someIdToDisplay);
Now it's clear what it's trying to do: the purpose is to show a form, not just create it.
See more on this question at Stackoverflow