简体   繁体   中英

C# how to add an object from one list to another

My issue is adding a specific student to a course that has been added to the List. The students are in class Student and List current. The courses are in class Course and List Courses. Everything else works great. This is just a basic Console App. If I was using SQL I wouldn't be having these issues but trying to learn C#.

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApp1
{
    public class Course
    {
        public string courseName { get; set; }
        public string teacherFirst { get; set; }
        public string teacherLast { get; set; }
        public List<Student> studentList;

        public static int courseID = 0;

        public static List<Course> Courses = new List<Course>();

        public Course(int ID, string name, string first, string last, List<Student> newStudentList)
        {
            ID = courseID;
            courseName = name;
            teacherFirst = first;
            teacherLast = last;
            studentList = newStudentList;
        }

        public static void addCourse()
        {
            courseID++;

            Console.Write("\nPlease enter the name of the course: "); // grabbing input from user.
            string name = Console.ReadLine();

            Console.Write("Please enter the teacher's first name: "); // grabbing input from user.
            string first = Console.ReadLine();

            Console.Write("Please enter the teacher's last name: "); // grabbing input from user.
            string last = Console.ReadLine();

            List<Student> newStudentList = new List<Student>();

            Course added = new Course(courseID, name, first, last, newStudentList);
            Courses.Add(added);
        }

        public static void printCourse()
        {
            foreach (var currentDirectory in Courses)
            {
                Console.WriteLine("\nCourse ID: {0}", courseID);
                Console.WriteLine("Course Name: {0}", currentDirectory.courseName);
                Console.WriteLine("Teacher's First Name: {0}", currentDirectory.teacherFirst);
                Console.WriteLine("Teacher's Last Name:  {0}\n", currentDirectory.teacherLast);
            }
        }

        public static void addStudentToClass()
        {
            Console.Write("Please enter the student's ID: ");
            string studentInput = Console.ReadLine();
            int studentID = Convert.ToInt32(studentInput);
            int studentIndex = Student.current.FindIndex(Student => Student.ID == (studentID));
            Console.WriteLine($"Student is located at index: {studentIndex} in the student list");

            Console.Write("Please enter the course ID: ");
            string courseInput = Console.ReadLine();
            int courseID = Convert.ToInt32(courseInput);
            int courseIndex = Course.Courses.FindIndex(Course => Course.courseID == (courseID));
            Console.WriteLine($"Course is located at index: {courseIndex} in the course list");

            // Trying to add a student from the current list in student to the Courses list in Course.
        }
    }
}

Take a look at LINQ . It will help you easily work with objects in collections. Eg

var course = Courses.FirstOrDefault(el => el.ID == CourseID);
var student = Students.FirstOrDefault(el => el.ID == studentID);
course.studentList.Add(student);

If you add the following to your addStudentToClass :

Courses[courseIndex].studentList.Add(Students.current[studentIndex]);

That should take your current code to where it needs to be. You may wish to push the LINQ approach anyway as clearly it's more compact.

Alternatively, if you wanted to tweak your design slightly, you could change your Student and Course collections into Dictionaries. This way your Course IDs and Student IDs can be directly used to access a particular Course/Student.

Dictionary<int, Course> Courses = new Dictionary<int, Course>();
...
addStudentToClass()
{
    Console.Write("Please enter the student's ID: ");
    string studentInput = Console.ReadLine();
    int studentID = Convert.ToInt32(studentInput);

    Console.Write("Please enter the course ID: ");
    string courseInput = Console.ReadLine();
    int courseID = Convert.ToInt32(courseInput);

    Courses[courseID].studentList.Add(Students.current[studentID]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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