Reputation: 313
New to multidimensional arrays and a bit stuck on a little project I am working on.
Getting a error when I try to store 2 arrays (student and teacher) in to a course array, when creating them as class arrays
Below is the code from my main()
Student stud1 = new Student("Alex", "Kelly", DateTime.Parse("14/07/2000"));
Student stud2 = new Student("Tom", "Smith", DateTime.Parse("16/08/198
Student stud3 = new Student("Mary", "Jones", DateTime.Parse("10/01/1998"));
//add students to an array and get the count
Student[] studentArray = new Student[3] { stud1, stud2, stud3 };
int count1 = studentArray.Length;
//add teacher objects
Teacher prof1 = new Teacher("Beckham");
Teacher prof2 = new Teacher("Pele");
Teacher prof3 = new Teacher("Maradonna");
Teacher[] teacherArray = new Teacher[3] { prof1, prof2, prof3 };
//course Object
Course course1 = new Course("Programming with C#");
Course[,] courseList = new Course[,] { {studentArray} , { teacherArray } };
I am getting the error on the last line shown, when I try to add the { studentArray, teacherArray }
to the courseList
array.
The error is
cannot implicitly convert type
Student[]
toCourse
If I change the array from Course
to object [,]
it works fine
Do I need to add something to my class file?
Upvotes: 0
Views: 1337
Reputation: 19407
The error you are experiencing is because Course
array will only allow objects of Course
type. You cannot place other objects or arrays with it.
To resolve your problem, it might be better to have the Student
and Teacher
arrays as a properties of the Course
object. These can then be assigned values as needed.
See:
https://msdn.microsoft.com/en-us/library/9b9dty7d.aspx for information on arrays. https://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx for information on properties.
Update following edit
The object[,]
array will work because object
is the base type for all other types. As a result of this, an object[]
can have any other type assigned to it. As you indicate you are learning to program so it might be worth reading up on object oriented design - It will help you model your data better. For a starting point try https://msdn.microsoft.com/en-us/library/dd460654.aspx.
Upvotes: 3
Reputation: 2522
To extend on a previous answers, always think about how you separate up your data. For example, the Course itself is a very 'static' concept compared to the attendees and the tutors. The Course could even have different dates with different attendees etc but will not change whereas dates and those involved will.
So your model could be:
public class CourseSchedule
{
public CourseSchedule(Course course, Student[] students, Teacher[] teacher)
{
this.Course = course;
....
}
// Some sort of Date too
public Course Course { get; private set; }
public IEnumerable<Student> Students { get; private set; }
public IEnumerable<Teacher> Teachers { get; private set; }
}
Multi-dimensional arrays are worth understanding but are specific to certain types of programming - graphics, mathematical transforms etc. You tend to use them for low level coding but for modelling real world structures such as your course booking they are generally not appropriate.
Upvotes: 0
Reputation: 498
What you should try to do is use OBJECT ARRAYS . This array will store the the different objects and it will also retain their form . Check out this link . Its quite detailed on this topic. enter link description here
Upvotes: 0
Reputation: 26856
It looks like you code can be refactored.
For example, why don't your Course
class look like:
class Course
{
public string Name;
public List<Student> Students;
public List<Teacher> Teachers;
}
It is more natural and object-orianted way. In this case you don't need two-dimensional array, and you can use only List<Course>
.
Also note - in many cases List<T>
is more convinient then T[]
array sincу it is resizing automatically.
Upvotes: 4