problems with inheritance???

  • Thread starter Thread starter Darryn Ross
  • Start date Start date
D

Darryn Ross

Hi..

I have a standard windows app in C#. The project contains a two forms and one class that i have created. I want both of the forms to inherit from the class but i am getting an error reading "type in interface list is not an interface"??? i am not sure how to fix the problem. Is an interface (form) unable to inherit from a non interface type??

any help appreciated
 
Darryn,
this is a little confusing because a "form" is not an interface, it is a class.
Perhaps with some code example we can get a better insight into what your goal really is.
You can't get the properties of a Windows Form class by having a class derive from another class that doesn't inherit itself from System.Windows.Forms.Form.
--Peter
Hi..

I have a standard windows app in C#. The project contains a two forms and one class that i have created. I want both of the forms to inherit from the class but i am getting an error reading "type in interface list is not an interface"??? i am not sure how to fix the problem. Is an interface (form) unable to inherit from a non interface type??

any help appreciated
 
Darryn Ross said:
The project contains a two forms and one class that i have created.
I want both of the forms to inherit from the class but i am getting an error

Form is a class, not an interface. Therefore, the form (which inherits
from System.Windows.Forms.Form) has already used up its one
permitted inheritance, and cannot simultaneously inherit from the
other class you have created.

C# only supports single inheritance, not multiple inheritance like C++.

The workaround is to aggregate this other class, such that your form
still inherits from form, but your form subclass must:

1. Contain an instance of the other class you've created in the
constructor as a member field.

2. Expose the methods and properties of the class you've created.

Aggregating the class, I'll call it T, in lieu of inheriting from it, would
look something like this:

public class F : Form
{
private T impl;
public F( ) { impl = new T( ); }
public int Foo( int x) { return impl.Foo( x); }
}

Normally, its desirable to make the methods and properties of the
aggregated class to be defined in an interface. Then anyplace where
you might ordinarily expect an instance of this class, you would use a
reference to this interface instead. The form class, instead of inheriting
from two classes, could inherit from one class and implement the interface
of the aggregated object (which supplies the actual implementation of
the interface and is delegated to by the form).

In a more interface or type-driven manner such as this, not having multiple
inheritance is not a problem.


Derek Harmon
 
Back
Top