Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I use 2 other classes on a base class. I have a class name subject ad it will contain the list of student class, subject name and teacher class

What I have tried:

I created student and teacher class that inherits from a person class now my problem is is storing student and teacher class on subject class.

  public class Student : Person
    {
        public Student(string FirstName, string LastName, string Gender) 
            : base(FirstName, LastName, Gender)
        {
            Console.WriteLine(FirstName, LastName, Gender);
        }
    }
}

public class Teacher : Person
    {
        public Teacher(string FirstName, string LastName, string Gender)
            : base(FirstName, LastName, Gender)
        {
            Console.WriteLine(FirstName, LastName, Gender);
        }
    }
}
Posted
Updated 22-Feb-17 19:58pm

1 solution

If a subject have to contain 2 members: 1 teacher and many students, a definition of sample class could looks like:
C#
public class Subject
{
    private List<Student> stu = new List<Student>();
    private Teacher tea = new Teacher();

    public List<Student> Students
    {
        get {return stu;}
        set {stu = value;}
    }

    public Teacher Teacher
    {
        get {return tea;}
        set {tea = value;}
    }
}


I'd suggest to read this article on MSDN: Walkthrough: Creating Your Own Collection Class[^]
 
Share this answer
 
v2
Comments
Member 13018326 23-Feb-17 2:06am    
thanks Maciej Los!
Maciej Los 23-Feb-17 2:08am    
You're very welcome.
Cheers,
Maciej
Maciej Los 23-Feb-17 2:21am    
Sorry, but it's your homework. I'd suggest to start with MSDN documentation. Use Google and type: "C# class msdn". When you get stuck, come back here and ask detailed question.
[Edit]
Don't get me wrong... I can give you a fish, but i'd rather give you a fishing rod.
Try!
Karthik_Mahalingam 23-Feb-17 12:18pm    
:)
Maciej Los 23-Feb-17 13:12pm    
Well, i understand that you'd say exactly the same ;)
Cheers!
Maciej

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900