-1

I need to create the classes Course, Teacher, Student, etc. The assignment also asked to "encapsulate" the data. The tricky part is that the Course class should contain an array of 3 student objects, and I really don't know how to do that.

This is part of the code(s) I have.

//Creating a Teacher class
using System;
namespace Homework_5
{
    class Teacher
    {
        private string _firstName;
        public string FirstName
        {
            get { return _firstName; }
            set { _firstName = value; }
        }
        private string _lastName;
        public string LastName
        {
            get { return _lastName; }
            set { _lastName = value; }
        }
    }
}


//Creating a Course class
using System;
namespace Homework_5
{
    class Course
    {
        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        private int _credits;
        public int Credits
        {
            get { return _credits; }
            set { _credits = value; }
        }
        private string _durationInWeeks;
        public string DurationInWeeks
        {
            get { return _durationInWeeks; }
            set { _durationInWeeks = value; }
        }
        //private Teacher array of 3. <--- I DON'T KNOW HOW TO DECLARE THIS
    }
}

The three Teacher objects are instantiated in main, along with the Course object. The Teacher objects should be passed to the Course object.

Nico
  • 12,493
  • 5
  • 42
  • 62
  • 1
    What exactly do you need to know? How to declare an [Array](https://msdn.microsoft.com/en-us/library/9b9dty7d.aspx)? Or something else? – vesan Feb 15 '17 at 02:26
  • You would need to obviously implement your Student class first and code its attributes. Then you could create an array and initialize it with the appropriate data. – Smitty Feb 15 '17 at 02:30

1 Answers1

1
private Teacher[] teachers = new Teacher[3];

Although I was reading your code not your text. I don't see your Student class. Also generally speaking using Array's is not the ideal collection as they are fixed width and cumbersome to redefine the number of elements. List is a much more common structure.

Jason Lind
  • 263
  • 1
  • 2
  • 15
  • How would I encapsulate the array, in order to make it accesible to main with accessors (get and set). I'm having trouble with that specifically... – Cristian Pool Linares Feb 15 '17 at 17:57
  • Well the easiest way would be to expose it like your other properties. However that enables consumers to change the width of the array. You might want to look into IEnumerable but that has consequences as well. – Jason Lind Feb 16 '17 at 18:43