Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Multiple Inheritance

Currently im studying the C# with ASP.NET MVC 4 with Code First Approach. Im Visual Basic Developer, and Now i want to Start C#. And, now i came accross the situation where i've to manage Multiple Inheritance. But, it is not possible with Class i thought. So, how should i manage these classes i have :

//I have the Following Person Class which Hold Common Properties 
//and a Type of Person e.g : Student, Faculty, Administrative
public class Person
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Type { get; set; }
}


//This is my Student Class, which is Derived from Person
public class Student : Person
{
    public DateTime DateOfBirth { get; set; }
    public DateTime EnrollmentDate { get; set; }
    public string Remarks { get; set; }

    public bool Approved { get; set; }
    public DateTime ApprovedDate { get; set; }
    public int ApprovedUserId { get; set; }
}


//This is my Faculty Class, which is also Derived from Person
public class Faculty : Person
{
    public DateTime HiredDate { get; set; }

    public bool Approved { get; set; }
    public DateTime ApprovedDate { get; set; }
    public int ApprovedUserId { get; set; }
}

What I want to Make is Approved, ApprovedDate, and ApprovedUserId also common. I want to Specify those Properties like :

 public class Approve {
    public bool Approved { get; set; }
    public DateTime ApprovedDate { get; set; }
    public int ApprovedUserId { get; set; }
}

And, want to use like :

public class Student : Person, Approve
{
    public DateTime DateOfBirth { get; set; }
    public DateTime EnrollmentDate { get; set; }
    public string Remarks { get; set; }
}

And, i cannot put those things inside PERSON. Because, i've to use this to another classes to, but those are not Person.

Then, how do i achieve this...

Please give me an Example for the Above Situation.

Please Help. And, Thank You so much in Advance.

like image 898
Nirmal Subedi Avatar asked Aug 26 '13 06:08

Nirmal Subedi


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


2 Answers

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; }
    }
}
like image 130
Paolo Tedesco Avatar answered Sep 27 '22 19:09

Paolo Tedesco


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
  }
like image 21
Dmitry Bychenko Avatar answered Sep 27 '22 21:09

Dmitry Bychenko