In my programming class, we are learning about Arraylists and structs. We were given an assignment where we would store multiple instances of the struct in the arraylist. The struct store a max value and a letter grade that corresponds to that value. For example, I create a struct called F. Its letter is F, and its max value is 299. Lets say I input 399 into the program. That should return C as the grade, but instead it returns F. I'm really unsure of what is causing this problem. Any help would be appreciated.
namespace StructAssignment
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ArrayList Grades = new ArrayList();
public struct Grade
{
public char letter;
public int maxPoints;
}
private void Form1_Load(object sender, EventArgs e)
{
Grade A;
A.letter = 'A';
A.maxPoints = 299;
Grades.Add(A);
Grade B;
B.letter = 'B';
B.maxPoints = 349;
Grades.Add(B);
Grade C;
C.letter = 'C';
C.maxPoints = 399;
Grades.Add(C);
Grade D;
D.letter = 'D';
D.maxPoints = 449;
Grades.Add(D);
Grade F;
F.letter = 'F';
F.maxPoints = 500;
Grades.Add(F);
}
private void button1_Click(object sender, EventArgs e)
{
string grade = string.Empty;
if(int.TryParse(textBox1.Text, out int points))
{
if(points < 0 || points > 500)
{
MessageBox.Show("Please enter a number greater than 0 and less than 500");
}
else
{
foreach(Grade gr in Grades)
{
if(points < gr.maxPoints)
{
grade = gr.letter.ToString();
}
}
MessageBox.Show(grade);
}
}
else
{
MessageBox.Show("Please enter a valid number!");
}
}
}
}