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