0

I have a list that looks like the following:

STUDENT: John, TEST ONE: A, TEST TWO: A, TEST THREE: D
STUDENT: Greg, TEST ONE: F, TEST TWO: B, TEST THREE: D

How can I delete the data for a specific student once the user inputs the name? I've tried doing the following but it doesn't work:

List<object> list = new List<object>(); // contains the list of students that
                                       //  were appended

Then, inside of a method here is some of the code that I wrote:

Console.Write("Enter the name of the student you want to remove: ");
string removedStudent = Console.ReadLine();
list.RemoveAll(s => s == removedStudent);

When the code runs and I input Greg as the name of the student I want to remove, nothing happens. I print the list and Greg is still in the list. So, instead of printing like this:

STUDENT: John, TEST ONE: A, TEST TWO: A, TEST THREE: D

It still prints like:

STUDENT: John, TEST ONE: A, TEST TWO: A, TEST THREE: D
STUDENT: Greg, TEST ONE: F, TEST TWO: B, TEST THREE: D

Is there a way I can delete the entire row just by specifying the name of the student?

Edit:

Here is the part where I ask the user to input the student information:

class StudentsIO
{
    public object getInput()
    {
        Console.Write("Enter student name: ");
        string studentName = Console.ReadLine();
        Console.Write("Enter test 1 score: ");
        string test1 = Console.ReadLine();
        Console.Write("Enter test 2: ");
        string test2 = Console.ReadLine();
        Console.Write("Enter test 3: ");
        string test3 = Console.ReadLine()
        Students students = new Students(studentName,test1,test2,test3);
        return students;
    }
}

Here's where I add students to the list:

StudentIO newStudent = new StudentIO();
object studentList = newStudent.getInput();
list.Add(studentList);
Kristian Damian
  • 1,360
  • 3
  • 22
  • 43
jo13
  • 209
  • 2
  • 3
  • 11
  • 5
    Why is it a list of object instead of a list of Student? – Ron Beyer Dec 03 '15 at 03:05
  • What is the content of `removedStudent` ?? – Mohit S Dec 03 '15 at 03:13
  • Linq should be `list.RemoveAll(s => s.STUDENT == removedStudent);` – juanvan Dec 03 '15 at 03:13
  • @RonBeyer I originally did have it as Student instead of object but my code threw a bunch of error, specifically when I tried appending a new student. It started working when I changed it to object. – jo13 Dec 03 '15 at 03:15
  • 2
    If you were getting errors when appending to `List` that is another issue that you need to address. It is seldom, if ever, a good idea to use `List`. If you find a need to use `List`, it usually means something is wrong with your design. – T Burnside Dec 03 '15 at 03:27
  • @joanne72205 Can you show us what you are doing when you are adding students? Are you, perchance, typing `list.Add("Greg")`? I'm wondering if you are inadvertently creating a list of strings _as objects_ instead of adding Student objects to your list. – Andrew Palmer Dec 03 '15 at 04:03
  • @AndyPalmer I added an edit where I copied some of the code that I used to ask the user various questions and then where I save it to the list. – jo13 Dec 03 '15 at 04:22
  • 1
    The signature of your method should be `public Students getInput()` and your assignment should be `Students studentLIst = newStudent.getInput()` or simply `var studentList = newStudent.getInput()`. Then you could have a `List`. – T Burnside Dec 03 '15 at 04:46
  • @TBurnside Your suggestion completely fixed all my problems! It makes sense now that I'm looking at the code with your edit and I don't know why I didn't implement it in the first place. – jo13 Dec 03 '15 at 05:10
  • @joanne72205, are you going to accept any of the answers? – T Burnside Dec 03 '15 at 06:19

3 Answers3

1

First, as Ron Beyer implies in his comment, you should be using something more specific than List<object>. I'm not sure what data type your students are, but let's say they are a class Student, with a STUDENT property that specifies the name. You would then have List<Student>.

Since the list contains objects (not Students), 1ist.removeAll(s => s == removedStudent) is using the == operator defined on object, which does object comparison. The string you entered is NOT the same object as the Student in the list. If you have a List<Student>, you would then use:

list.removeAll(s => s.STUDENT == removedStudent);

to remove the student from the list.

T Burnside
  • 121
  • 6
0

I can't tell what your object looks like from your question, but have you tried something like this?

list.RemoveAll(s => s.STUDENT == removedStudent);

Answered here as well

Community
  • 1
  • 1
BSSchwarzkopf
  • 352
  • 2
  • 11
0

I'd suggest:

students.RemoveAll(s => string.Compare(s.Name, studentNameToRemove, true) == 0);
Tathagat Verma
  • 549
  • 7
  • 12