将对象数组添加到另一个类c#

时间:2018-11-08 21:14:00

标签: c# class subclass

我正在尝试读取以下格式的文本文件: (最后的#号只是他们所在课程的数量,但我没有将课程名称保存在fac / students类中)

Course Biology
Faculty Taylor Nate 0
Student Doe John 3
Student Sean Big 0
Course Art
Faculty Leasure Dan 1

首先输入的应该是一门课程,然后是该特定课程的教职员工。课程课程应包含教师和学生的集合。

我已经能够将每门课程/学生/教师放到各自的班级,但是我很难想象将学生/教师添加到课程中的方法。

我当前的想法是将数据放入各自的类中,以便保留课程的当前索引,因此我将其另存为

courses[currentCourse++]

因此,当我解析下一行时,(作为教师/学生)我已经知道课程索引应该是什么。

        using (StreamReader reader = new StreamReader(fileName))
        {
            while (!reader.EndOfStream)
            {
                lineCounter++;
                line = reader.ReadLine();
                string[] words = line.Split(' ');
                Console.WriteLine(words[0]);
                if (words[0] == "Course")
                    {
                        string nameOfCourse = words[1];
                        courses[currentCourse++] = new Course
                        {
                            Name = nameOfCourse
                        };
                    }
                    if (words[0] == "Faculty")
                    {
                        string firstName = words[1];
                        string lastName = words[2];
                        string numOfClasses = words[3];
                        faculty[currentFaculty++] = new Faculty
                        {
                            FirstName = firstName,
                            LastName = lastName,
                            NumOfClasses = numOfClasses,
                        };
                    }
                    if (words[0] == "Student")
                    {
                        string firstName = words[1];
                        string lastName = words[2];
                        string numOfClasses = words[3];
                        students[currentStudent++] = new Student
                        {
                            FirstName = firstName,
                            LastName = lastName,
                            NumOfClasses = numOfClasses,
                        };
                    }

我知道问题出在课程班级本身,但是我不确定将班级添加到另一个班级的术语。

public class Course
{
    public override string ToString()
    {
        return $"{Name}";
    }
    public string Name { get; set; }

}

public class Student
{
    public override string ToString()
    {
        return $"{FirstName} {LastName} {NumOfClasses}";
    }
    public string FirstName { get; set; } = string.Empty;

    public string LastName { get; set; } = string.Empty;

    public string NumOfClasses { get; set; } = string.Empty;
}

感谢阅读!

2 个答案:

答案 0 :(得分:1)

您要向课程添加StudentFaculty的集合,对吗?只需将List<T>添加到Course类中,然后在构造函数中对其进行初始化,就可以做到这一点。

public class Course
{
    public override string ToString()
    {
        return $"{Name}";
    }
    public string Name { get; set; }

    public List<Student> Students { get; set; }

    public List<Faculty> FacultyMems { get; set; }

    public Course()
    {
        Students = new List<Student>();
        FacultyMems = new List<Faculty>();
    }

}

在您的using块中,您可以按以下方式将每个学生/教职员工添加到课程中:

if (words[0] == "Course")
{
   string nameOfCourse = words[1];
   currentCourse++;
   courses[currentCourse] = new Course
   {
      Name = nameOfCourse
   };
}
if (words[0] == "Faculty")
{
    string firstName = words[1];
    string lastName = words[2];
    string numOfClasses = words[3];
    courses[currentCourse].FacultyMems.Add(new Faculty
    {
        FirstName = firstName,
        LastName = lastName,
        NumOfClasses = numOfClasses,
    });
}
if (words[0] == "Student")
{
    string firstName = words[1];
    string lastName = words[2];
    string numOfClasses = words[3];
    courses[currentCourse].Students.Add(new Student
    {
        FirstName = firstName,
        LastName = lastName,
        NumOfClasses = numOfClasses,
    });
}

这样,每次遇到"Course"时,您的课程列表都会添加一个新项目,然后您可以在出现这些值时附加学生/教师/等。

这可以进一步简化,但是这里有一个概念供您遵循。希望这会有所帮助。

答案 1 :(得分:0)

如果我对您的理解正确,那么您是否希望course列出教师和学生的名单?

public class Course

{
    public override string ToString()
    {
        return $"{Name}";
    }
    public string Name { get; set; }
    public List<Student> Students { get; set; }
    public List<Faculty> FacultyMembers {get; set;} 

}

请确保在尝试将列表添加到列表之前初始化列表,否则您将获得null引用异常。