Reputation: 3
I am trying to make a loop from a list based in a class, but im receiving this error : for cannot apply indexing with [] to an expression of type 'int'
the point is, do a list (this list must it have a 1 class) for to register in the list the names and qualifications of students. but in the loop this not working good (the text in black cursive is actually in red in my code)
public class Register
{
public string Name{ set; get; }
public int Note{ set; get; }
public string ask()
{
List<Register> Students = new List<Register>();
var student= new Register();
string question;
var r1 = "si";
Console.WriteLine("Desea agregar un estudiante? ");
question= Console.ReadLine();
while(question == r1)
{
Console.WriteLine("Cual es el nombre del estudiante?: ");
student.Name= Console.ReadLine();
Console.WriteLine("Cual es la nota del estudiante?: ");
student.Note = int.Parse(Console.ReadLine());
Students.Add(student);
Console.WriteLine("Desea agregargar otro estudiante?: ");
question = Console.ReadLine();
}
for (int i = 1; i < Students.Count; i++)
{
if (***student.Note[i]*** >= 70)
{
Console.WriteLine($"{***student.Name[i]***} ha aprobado con {***student.Note[i]***}");
}
else
{
Console.WriteLine($"{***student.Name[i]***} ha reprobado con {***alumno.Nota[i]***}");
}
}
return Console.ReadLine();
}
}
Upvotes: 0
Views: 1378
Reputation: 6164
Students is the list. Note is a property of the Register objects in that list. Therefore in order to access a specific element of the Students list, you need to use the indexer on it, not the property. So it would look like this:
if (Students[i].Note >= 70)
{
....
Similarly the Console.WriteLine statements would also need to use Students[i].Name instead of student, which was a single object.
Upvotes: 2
Reputation: 2796
student.Name[i]
should be student[i].Name
.
if (student[i].Note >= 70)
{
Console.WriteLine($"{student[i].Name} ha aprobado con {student[i].Note}");
}
else
{
Console.WriteLine($"{student[i].Name} ha reprobado con {alumno[i].Nota}");
}
Upvotes: 1