0

I have a list of Students like below

public List<Students> StudentsClassCollection;

Students class looks like below:

public class Students
{
    public string StudentName;
    public bool Passed;
}

I want to access one of students and change the value of Passed from false to true. How can i do that?

etrupja
  • 2,710
  • 6
  • 22
  • 37
  • Is _StudentName_ an unique value between your students? If not, how do you plan to correctly identify between a set of students all with the same name? – Steve Jun 30 '16 at 13:45
  • when you say students class every one that will develop in that code will think that this class have list of student. wrong name, i posted answer – Leon Barkan Jun 30 '16 at 13:56

4 Answers4

3

Use Linq to identify the student by name and set the property to true. Using just a name is a terrible way of finding a record as you need an exact match (include the case of letters). Add a primary key or break up the name to first and last and include a date of birth.

    public void UpdateStudentToPassed(string studentName)
    {
       StudentsClassCollection.Single(obj => obj.StudentName == studentName).Passed = true;
    }
Michael
  • 188
  • 6
2
var foundStudent = StudentsClassCollection.First(s => s.StudentName == "LookingForYou");
foundStudent.Passed = true;
Yahya
  • 3,386
  • 3
  • 22
  • 40
  • Of course if there is no student with that name this will crash spectacularly – Steve Jun 30 '16 at 13:48
  • @Steve there were a few assumptions made. When I am certain about my data, I use First, don't need to be doubtful and use FirstOrDefault. – Yahya Jun 30 '16 at 13:50
1

If you know which student, by index, it's as simple as:

StudentsClassCollection[i].Passed = true

If you're looking for the student by name, perhaps:

var student = StudentsClassCollection.FirstOrDefault(s => s.name == "Bob");
if (student != null) {
 student.Passed = true
}

for the student "Bob"

Jaydip Jadhav
  • 12,179
  • 6
  • 24
  • 40
daf
  • 1,289
  • 11
  • 16
  • If there is just one "Bob" otherwise ? – Steve Jun 30 '16 at 13:50
  • If there are multiple students with the same name, then the system has bigger issues -- how do you tell which one actually passed? If it's valid behaviour that all students called "Bob" are to pass, you could: `foreach (var student in StudentsClassCollection.Where(s => s.name == "Bob") { student.passed = true; }` – daf Jun 30 '16 at 13:52
0

One of the most impotent things in oop is doing the base stuff class should be called Student and not students (you can see it in your case that when you want it to be students you doing list of Student)

    public List<Student> StudentsClassCollection = new List<Students>();
    StudentsClassCollection.Add(new Student("Ben","true"))

    public class Student
    {
        public string StudentName {get; set;}
        public bool Passed { get; set;}

        public Student(string name,Bool pass)
        {
           this.StudentName = name;
           this.Passed = pass;
        }
    }

    foreach(Student s in StudentsClassCollection)
    {
       if(s.StudentName.Equals("what you looking for"))
          s.Passed = true;
    }
Leon Barkan
  • 2,676
  • 2
  • 19
  • 43