Question

so..I'm trying to build a "school schedule optimizer" My program recieves a .dat file containing the info. of th student including each subject. The problem is that I want to make a class Student.cs and to make the constructor equal to the number of subjects. I don't know what to do because the student could have from 0 to (theorically) infinite number of subjects. Is there a way to make a constructor or define a class depending on a variable? Or any idea on how to get this student into an array without having any out of bounds problems

Was it helpful?

Solution

You need to look at the factory pattern. Instantiate the student class devoid of any subjects, then call the school subject factory for each subject that the student wants to register in. All this could be encapsulated within the Studen t constructor.

OTHER TIPS

You can pass in a collection of subjects into the Student constructor like this:

public Student(IEnumerable<string> subjects)
{
}

public Student(params string[] subjects)
{
}

Or when Subject is an class on itself:

public Student(IEnumerable<Subject> subjects)
{
}

public Student(params Subject[] subjects)
{
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top