C# Multiple Inheritance
One possible solution would be to modify your hierarchy:
public class PersonWithApprove : Person { // TODO: replace with non disgusting name
public bool Approved { get; set; }
// etc...
}
public class Student : PersonWithApprove {
}
public class Faculty : PersonWithApprove {
}
Or you could create an interface:
public interface IApprove {
bool Approved { get; set; }
// etc
}
public class Student : Person, IApprove {
}
You might also leave the class Approve
as such, and have classes with a property of that type:
public class Student : Person {
Approve _approve = new Approve();
public Approve Approve {
get { return _approve; }
}
}
It's a good case, IMHO, to use interfaces here, something like that:
// Interfaces:
// General person
public interface IPerson {
int Id { get; set; }
string FirstName { get; set; }
string LastName { get; set; }
string Type { get; set; }
}
// Approvable person
public interface IApprovable {
bool Approved { get; set; }
DateTime ApprovedDate { get; set; }
int ApprovedUserId { get; set; }
}
// Student is a IPerson + IApprovable
public interface IStudent: IPerson, IApprovable {
DateTime DateOfBirth { get; set; }
DateTime EnrollmentDate { get; set; }
}
// So classes will be
public class Approve: IApprovable {
... //TODO: Implement IApprovable interface here
}
public class Faculty: IPerson, IApprovable {
public DateTime HiredDate { get; set; }
... //TODO: Implement IPerson interface here
... //TODO: Implement IApprovable interface here
}
public class Student: IStudent {
public string Remarks { get; set; }
... //TODO: Implement IStudent interface here
}
Short Answer
Consider using interfaces instead, which allow for multiple inheritance and can be declared using the interface
keyword.
Long Answer
Inheritance from multiple base classes in C# is illegal. Classes may only have 1 base class while they can implement any number of interfaces. There are several reasons for this but it mostly comes down to that multiple inheritance introduces much more complexity into a class hierarchy.
Interfaces are used to declare a group of common functionality (methods and properties) that must be implemented by class.
To modify your existing code to use interfaces (instead of multiple inheritance), you can do the following:
public interface IApprove // Defines a set of functionality that a class must implement.
{
// All these properties must be inherited as public when implemented.
bool Approved { get; set; } // Property declaration.
DateTime ApprovedDate { get; set; }
int ApprovedUserId { get; set; }
}
public class Student : Person, IApprove
{
public DateTime DateOfBirth { get; set; }
public DateTime EnrollmentDate { get; set; }
public string Remarks { get; set; }
#region IApprove Implementation
private bool _approved; // Private variable that is accessed through the 'Approved' property of the 'IApprove' interface.
public bool Approved // Defines 'Approved' inherited from IApprove
{
get { return _approved; }
set { _approved = value; }
}
private DateTime _approvedDate;
public DateTime ApprovedDate // Defines 'ApprovedDate' inherited from IApprove.
{
get { return _approvedDate; }
set { _approvedDate = value; }
}
private int _approvedUserId;
public int IApprove.ApprovedUserId // Alternative syntax to define an interfaces property.
{
get { return _approvedUserId; }
set { _approvedUserId = value; }
}
#endregion
}
This approach abstracts the implementation of an IApprove interface and, like multiple inheritance, allows the user to operate on objects that implement IApprove yet their concrete type is unknown (or irrelevant).
For more information on the usage of interfaces in C# refer to:
http://msdn.microsoft.com/en-us/library/87d83y5b.aspx