none
How to keep adding to a List<T> then refresh RRS feed

  • Question

  • I've made a console application which is a roster for a class room. I've  added options to remove Students and add Students to the Classroom101 List. At first I manually typed the index at which id like to assign the FirstName and IdNumber values which worked. I noticed that if I added another student the previous entry would get over written because it was just editing the the previous entry. I then tried to use a for loop and use variable i as the index but the loop would keep going because the list would keep growing. Then I tried to use a foreach statement and when I did this there was a error at runtime saying "Collection was Modified". My question is how would I be able to repeatedly add to the list and keep the changes even after the program closes out.

    foreach(var student in ClassRoom101)
                        {
                            int i = 4;
                            ClassRoom101.Add(new Students());
                            Console.WriteLine("Please  enter an ID number for student that is not already in use.");
                            string UserInputStudentID = Console.ReadLine();
                            ClassRoom101[i].StudentId = Int32.Parse(UserInputStudentID);
                            Console.WriteLine("Please enter students first name");
                            string UserInputFirstName = Console.ReadLine();
                            ClassRoom101[i].FirstName = UserInputFirstName;
                            i++;
                        }
    Exception at runtime: "Unhandled Exception: System.InvalidOperationException: Collection was modified; enumeration operation may not execute."
    Tuesday, September 11, 2018 4:35 AM

Answers

  • Greetings again.

    You didn't really answer my questions, but I think I can guess what you are looking for.

    Try replacing this...

                        foreach(var student in ClassRoom101)
                        {
                            int i = 4;
                            ClassRoom101.Add(new Students());
                            Console.WriteLine("Please  enter an ID number for student that is not already in use.");
                            string UserInputStudentID = Console.ReadLine();
                            ClassRoom101[i].StudentId = Int32.Parse(UserInputStudentID);
                            Console.WriteLine("Please enter students first name");
                            string UserInputFirstName = Console.ReadLine();
                            ClassRoom101[i].FirstName = UserInputFirstName;
                            i++;
                        }

    ... with this.

                            Student student = new Student();
                            ClassRoom101.Add(student);
                            Console.WriteLine("Please  enter an ID number for student that is not already in use.");
                            string UserInputStudentID = Console.ReadLine();
                            student.StudentId = Int32.Parse(UserInputStudentID);
                            Console.WriteLine("Please enter students first name");
                            string UserInputFirstName = Console.ReadLine();
                            student.FirstName = UserInputFirstName;
     

    That is, no loop and no i.

    • Marked as answer by Rechtfertigung Tuesday, September 11, 2018 5:31 AM
    Tuesday, September 11, 2018 5:12 AM

All replies

  • P.s the reason why i is 4 is because there is already ClassRoom101[0]-[3]
    Tuesday, September 11, 2018 4:39 AM
  • Greetings Rechtfertigung.

    I'm confused about what you are trying to achieve.

    That code looks very wrong. Is it trying to add a new student, or replace an existing student? And what is the loop supposed to achieve?

    Tuesday, September 11, 2018 4:55 AM
  • This is the full code. And I know how to replace values but i'd like to be able to keep adding new values to the list and keep the changes made even after the program is closed.

    Full code:

    namespace ListT
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<Students> ClassRoom101 = new List<Students>()
                {
                    new Students() { StudentId = 0, FirstName = "Dom"},
                    new Students() { StudentId = 1, FirstName = "Rob"}
                };
    
                ClassRoom101.Add(new Students() { StudentId = 2, FirstName = "jack" });
                ClassRoom101.Add(new Students() { StudentId = 3, FirstName = "bob" });
    
                for(int i=0; i<ClassRoom101.Count;i++ )
                {
                    Console.WriteLine(ClassRoom101[i].FirstName + " " + i);
                }
                while(true)
                {
                    Console.WriteLine("If you would like ot add new students to the class press N if youd like to remove a student please press R");
                    string UserEditUserAnswer = Console.ReadLine().ToUpper();
                    if (UserEditUserAnswer == "N")
                    {
                        foreach(var student in ClassRoom101)
                        {
                            int i = 4;
                            ClassRoom101.Add(new Students());
                            Console.WriteLine("Please  enter an ID number for student that is not already in use.");
                            string UserInputStudentID = Console.ReadLine();
                            ClassRoom101[i].StudentId = Int32.Parse(UserInputStudentID);
                            Console.WriteLine("Please enter students first name");
                            string UserInputFirstName = Console.ReadLine();
                            ClassRoom101[i].FirstName = UserInputFirstName;
                            i++;
                        }
                    }
                    else if (UserEditUserAnswer == "R")
                    {
                        Console.WriteLine("Please select number of student you want to remove:");
                        string UserInputRemoveStudent = Console.ReadLine();
                        ClassRoom101.Remove(ClassRoom101[Int32.Parse(UserInputRemoveStudent)]);
    
                    }
                    Console.WriteLine("New roster:\n");
                    for (int i = 0; i < ClassRoom101.Count; i++)
                    {
                        Console.WriteLine(ClassRoom101[i].FirstName + " " + i);
                    }
                }
            } 
        }
        class Students
        {
            public int StudentId { get; set; }
            public string FirstName { get; set; }
        }
    }


    Tuesday, September 11, 2018 5:06 AM
  • Greetings again.

    You didn't really answer my questions, but I think I can guess what you are looking for.

    Try replacing this...

                        foreach(var student in ClassRoom101)
                        {
                            int i = 4;
                            ClassRoom101.Add(new Students());
                            Console.WriteLine("Please  enter an ID number for student that is not already in use.");
                            string UserInputStudentID = Console.ReadLine();
                            ClassRoom101[i].StudentId = Int32.Parse(UserInputStudentID);
                            Console.WriteLine("Please enter students first name");
                            string UserInputFirstName = Console.ReadLine();
                            ClassRoom101[i].FirstName = UserInputFirstName;
                            i++;
                        }

    ... with this.

                            Student student = new Student();
                            ClassRoom101.Add(student);
                            Console.WriteLine("Please  enter an ID number for student that is not already in use.");
                            string UserInputStudentID = Console.ReadLine();
                            student.StudentId = Int32.Parse(UserInputStudentID);
                            Console.WriteLine("Please enter students first name");
                            string UserInputFirstName = Console.ReadLine();
                            student.FirstName = UserInputFirstName;
     

    That is, no loop and no i.

    • Marked as answer by Rechtfertigung Tuesday, September 11, 2018 5:31 AM
    Tuesday, September 11, 2018 5:12 AM
  • Sorry for the vague answer. Your solution worked is there a way to save the new changes so next time the program is opened the list edited is still there and not reset to its original contents. I was thinking about saving the changes made into a txt file an then every time the application is opened having the contents of the text file loaded into the list.
    Tuesday, September 11, 2018 5:31 AM
  • A text file would work. So would a database table, if you want to be really clever.

    I would recommend the text file for a beginner.

    Tuesday, September 11, 2018 5:36 AM
  • I am learning MS SQL Server but I cant find many helpful links that show you how to connect it to a regular application most of it I see is using it for web solutions. However I didn't spend much time on researching it so I'll put some more time in. Again thanks!
    Tuesday, September 11, 2018 5:40 AM
  • Not sure what you are expecting. In case, if you want to keep the list and it's changes even after closing the c# program, then you need to save those list details in sql database or other database and fetch details before you perform any operations.

    Tuesday, September 11, 2018 5:45 AM
  • I think it would be helpful to decouple the student id and the index in the list. Let C# keep track of the index and the count. You can then display the entire list with a foreach().

    When you add a student, you can assign a new (unused) id. (For extra credit, keep the last used id # in a separate file. When you add a student, read & increment this # and store the updated value in the file.) List.Add() will add it to your list and you can then go on to the next one as Ante Meridian shows below. The index of the new student is not important. 

    To delete a student, List.Remove().

    Once you're done editing, save your updated list.

    Thursday, September 13, 2018 6:15 PM