0

So im trying to fill the courses on my program and when I click the Input button instead of proceeding to the next student I get this error "An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll"

Here is the button

namespace WpfApplication1{ public partial class MainWindow : Window {

    List<Students> student = new List<Students>();
    int positionIndex = 0;

    public MainWindow() {

        InitializeComponent();     
    }

    void btnExe_Click(object sender, RoutedEventArgs e) {
        var course1 = Convert.ToDouble(txtCourse1.Text);
        var course2 = Convert.ToDouble(txtCourse2.Text);
        var course3 = Convert.ToDouble(txtCourse3.Text);
        student[positionIndex].c1 = course1;
        student[positionIndex].c2 = course2;
        student[positionIndex].c3 = course3;

        student[positionIndex].Name = positionIndex;
        stdNames.Text = student[positionIndex].Name.ToString();
        positionIndex++;
        if (positionIndex == 6) {
            btnExe.IsEnabled = false;
            calculate();
        };
    }

Here is the class

namespace WpfApplication1 {

public class Students {
    public int Name { get; set; }
    public double c1 { get ; set; }
    public double c2 { get; set; }
    public double c3 { get; set; }
}

}


Thanks in advance, I really can't figoure out what is wrong.

1 Answers1

0

Create an instance before accessing the item. student.Add(new Students());

void btnExe_Click(object sender, RoutedEventArgs e) {
        var course1 = Convert.ToDouble(txtCourse1.Text);
        var course2 = Convert.ToDouble(txtCourse2.Text);
        var course3 = Convert.ToDouble(txtCourse3.Text);
        student.Add(new Students());

        student[positionIndex].c1 = course1;
        student[positionIndex].c2 = course2;
        student[positionIndex].c3 = course3;

        student[positionIndex].Name = positionIndex;
        stdNames.Text = student[positionIndex].Name.ToString();
        positionIndex++;
        if (positionIndex == 6) {
            btnExe.IsEnabled = false;
            calculate();
        };
    }
loopedcode
  • 4,863
  • 1
  • 21
  • 21