简体   繁体   English

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

[英]Adding an Array of Objects to another Class c#

I am attemping to read a text file in the format of (The # at end is just the number of classes they're in, but I dont save the course name with the fac/students class) 我试图读取以下格式的文本文件(结尾的#只是他们所在课程的数量,但我没有将课程名称保存在fac / students类中)

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

The first input should be a course, followed by the faculty and students of the specific course. 首先输入的应该是一门课程,然后是特定课程的教职员工。 The Course class should contain a collection of faculty members and a collection of students. 课程课程应包含教师和学生的集合。

I have been able to put each course/student/faculty into their respective class, but I am having trouble visualizing a way to add the students/faculty to the course. 我已经能够将每门课程/学生/教师放到各自的班级,但是我在想像一种将学生/教师添加到课程中的方法时遇到了麻烦。

My current idea putting the data into their respective classes would be to keep the current index of the course- therefore I have it saved as 我目前的想法是将数据放入各自的类中,以便保留课程的当前索引,因此我将其另存为

courses[currentCourse++]

so when I parse the next line, (being a faculty/student) I already know what the course index should be. 因此,当我解析下一行时,(作为教师/学生)我已经知道课程索引应该是什么。

        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,
                        };
                    }

I know the problem lies in the courses class itself- but i'm not sure the terminology to add a class to another class. 我知道问题出在课程班级本身,但是我不确定将班级添加到另一个班级的术语。

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;
}

Thanks for reading! 谢谢阅读!

You want to add a collection of Student and Faculty to the course class, correct? 您要向课程添加StudentFaculty的集合,对吗? You can do so like this by simply adding a List<T> to your Course class and then initializing it in a constructor. 您可以通过将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>();
    }

}

And in your using block, you can add each student/faculty to the course as so: 在您的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,
    });
}

With this, each time you encounter "Course" your course list will add a new item and then you can append students/faculty/etc when those values occur. 这样,每次您遇到"Course"您的课程列表都会添加一个新项目,然后您可以在出现这些值时追加学生/教师/等。

This can be simplified even further but the concept is there for you to follow. 这可以进一步简化,但是这里有您可以遵循的概念。 Hope this helps. 希望这可以帮助。

If I'm understanding you correctly, you want your course s to have a list of faculty and students? 如果我对您的理解正确,那么您是否希望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;} 

}

Just be sure to initialize the Lists before trying to add things to them otherwise you'll get a null ref exception. 只需确保在尝试将列表添加到列表之前初始化列表,否则您将获得null ref异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM