The task is as follows: depending on the value of Enum in DC.DocType, create various forms of WPF, and then work with them uniformly. Forms are not connected by an interface, and I am going to use only the basic members of WPF forms, such as ShowDialog ()

I am trying to do something like this, but then the conversion to the desired type fails. Explain, please, how to solve this problem?

object dlg; Type tp; switch (DC.DocType) { case QuestionnairesDocTypes.FormV: dlg = new CreateFormV1View(); tp = typeof(CreateFormV1View); break; case QuestionnairesDocTypes.FormL: dlg = new CreateFormL1View(); tp = typeof(CreateFormV1View); break; // ... } if ((dlg as tp).ShowDialog() == true) { // .... } 

    1 answer 1

    Set the dlg variable to the dlg type. Then no type conversions will be needed:

     Window dlg; switch (DC.DocType) { case QuestionnairesDocTypes.FormV: dlg = new CreateFormV1View(); break; case QuestionnairesDocTypes.FormL: dlg = new CreateFormL1View(); break; // ... } if (dlg.ShowDialog()) { // .... } 
    • These are WPF forms. They are not compatible with the System.Windows.Form type. In your version, you need to specify the specification for the Form, and all that comes to mind is to specify System.Windows. And then initialization is impossible. - Andrey Kutasevich
    • @AndreyKutasevich So that there would be no such confusion, you should immediately call everything your tongue. In WPF, there are no "forms (Form)", there are "windows (Window)" ... - EvgeniyZ
    • Eugene, thank you! Your answer clarification helped solve the problem. - Andrei Kutasevich