Visual Studio does not interpret a class that 'inherits' from Form as such

0

From what I understand, a form can only inherit from Form , it can not have multiple inheritances. That is, the following could not be done:

public partial class frmCliente : Form,  BaseMaestro<Cliente>

However, if I do the following:

public abstract class BaseMaestro<T> : Form

And also frmCliente is another class that, in turn, inherits from the previous one:

public partial class frmCliente : BaseMaestro<Cliente>

So that frmCliente would be inheriting from Form .

However, VS does not interpret it as such anymore and I can not add controls using the designer. Is there any way to solve this small inconvenience?

The designer can not be shown for this file because none of the classes it contains can be designed. The designer inspected the following classes in the file:

frmCliente --- La clase base 'Example.WindowsForms.Clases.BaseMaestro<Example.EntidadesDominio.Cliente>'

could not be loaded. Make sure that the assembly has been referenced and that all projects were generated.

I get this error

    
asked by Pedro Ávila 15.10.2016 в 00:49
source

1 answer

2

It is valid to extend the Form class, but the Visual Studio designer requires that the base be an instanceable class.

In your code BaseMaestro<T> is an abstract class, which means that it can not be instantiated. This only affects the VS graphical editor because it works internally, so you could program your class in the code editor and compile, but I do not think that's what you're looking for.

One way to avoid this problem is to define a class that inherits from the abstract before:

public class Maestro<T>: BaseMaestro<T>

And use that class in your way:

public partial class frmCliente : Maestro<Cliente>

But with this you have to declare everything in the new class and you lose the advantage of using an abstract class.

If you defined it to have code in common, you could simply inherit from a normal class, without making it abstract.

public class BaseMaestro<T> : Form

As you mention using multiple inheritances, depending on what you need it could be that it suits you better to use interfaces. A class can implement more than one interface, in your case you could do:

public partial class frmCliente : Form, InterfazForma1<Cliente>, InterfazForma2<Cliente>

Or even a mixture of both solutions:

public partial class frmCliente : BaseMaestro<Cliente>, InterfazForma<Cliente>

Herendando the common functions of BaseMaestro and fulfilling the specific of each form according to the defined in the interface.

Here is more information on when to choose one or the other: link

    
answered by 15.10.2016 / 04:22
source