0

I have designed 3 different win forms in c#.

public partial class SLOSPR : form
{
    public bool IsCallable;
    public void PopulateData();
}
public partial class SLOFIX : form
{
    public bool IsCallable;
    public void PopulateData();
}
public partial class SPDBID : form
{
    public bool IsCallable;
    public void PopulateData();
}

I have declared one member variable IsCallable in every form's class and a member function PopulateData() in every form's class. It is so because, in a code segment I want to declare one variable of a form type, and want to assign an object of any of the above 3 classes and use that IsCallable variable and call PopulateData().

like this:

public form RetForm()
{
     form frm=new /* constructor of any 3 forms mentioned above*/
     //this function can return any of the three types which will be decided at run time.
     return frm;

}
form frm=RetForm();

here i want to use IsCallable and call PopulateData().

like this:

if(frm.Iscallable)
    frm.PopulateData();

Which is not possible syntactically as Iscallable and PopulateData is not member of form class

To solve this problem I extended the form class to an abstract class and declared those 3 forms from the extended class.

public abstract class EXTENDED_FORM : form
{
     public bool IsCallable;
     public abstract void PopulateData();
}

and changed the definition of those 3 forms like this.

public partial class SLOSPR : EXTENDED_FORM
{
    public override void PopulateData()
    {
       /*body for SLOSPR */
    }
}
public partial class SLOFIX : EXTENDED_FORM
{
   public override void PopulateData()
    {
       /*body for SLOFIX */
    }
}
public partial class SPDBID : EXTENDED_FORM
{
   public override void PopulateData()
    {
       /*body for SPDBID*/
    }
}

Now I did like this:

public EXTENDED_FORM RetForm()
{
     EXTENDED_FORM frm=new /* constructor of any 3 forms mentioned above*/
     //this function can return any of the three types which will be decided at run time.
     return frm;

}
EXTENDED_FORM frm=RetForm();

Now I can call like this:

if(frm.Iscallable)
    frm.PopulateData();

Automatically overridden PopulateData will be called.

Finally I made my purpose. But as I changed

 public partial class SLOSPR : form       /*[Designer generated code]*/

to

 public partial class SLOSPR : EXTENDED_FORM

The GUI designer in Visual Studio gets messed up, showing the following page.

How to get rid of this, also my purpose gets fulfilled without hampering the designer???

Ankur
  • 330
  • 2
  • 8
  • 17
  • 1
    You will need to find out why it is crashing. To do so, open two instances of Visual Studio. Attach the first instance's debugger to the second instance (via Debug | Attach to process). In the second instance, open the form in the designer so that the error occurs. Now the first instance should break into the debugger to show you the code that's causing the issue. – Matthew Watson Jan 30 '15 at 11:24
  • 2
    See duplicate, about the first Google result if you start typing _"The designer cannot create an instance abstract"_. You can also just use an interface on your forms, if the abstract class doesn't contain an implementation. – CodeCaster Jan 30 '15 at 11:25
  • You could do it via interfaces, too... http://i.imgur.com/Ephj3H3.png – Mikko Viitala Jan 30 '15 at 11:35

1 Answers1

2

don't make EXTENDED_FORM abstract; remove abstract from its declaration and add protected constructor without parameters

ASh
  • 34,632
  • 9
  • 60
  • 82
  • This unfortunately; we had to deal with abstract base forms in my previous company (legacy code). Those problems are a bit nondeterminate, sometimes a solution clean, closing VS and rebuild would fix this, sometimes you needed to do it twice/thrice. I'm not sure any valid workaround exists - so I'd go with this answer. – decPL Jan 30 '15 at 11:27