C#-当我在运行程序时添加一个数字时,出现以下错误';System.ArgumentOutOfRangeException';发生在mscorlib.dll中;
因此,我试图在我的程序中填充课程,当我单击输入按钮而不是继续下一个学生时,我得到了以下错误:“mscorlib.dll中发生了类型为'System.ArgumentOutOfRangeException'的未处理异常” 这是按钮 命名空间WpfApplication1{ 公共部分类主窗口:窗口{C#-当我在运行程序时添加一个数字时,出现以下错误';System.ArgumentOutOfRangeException';发生在mscorlib.dll中;,c#,visual-studio,C#,Visual Studio,因此,我试图在我的程序中填充课程,当我单击输入按钮而不是继续下一个学生时,我得到了以下错误:“mscorlib.dll中发生了类型为'System.ArgumentOutOfRangeException'的未处理异常” 这是按钮 命名空间WpfApplication1{ 公共部分类主窗口:窗口{ List<Students> student = new List<Students>(); int positionIndex = 0; public
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();
};
}
}
提前感谢,我真的无法找出问题所在。在访问项目之前创建一个实例。
student.Add(newstudents());
你从来没有在
学生
列表中添加任何内容,所以它是空的,因此索引0中没有任何内容。你们真是太快了
public class Students {
public int Name { get; set; }
public double c1 { get ; set; }
public double c2 { get; set; }
public double c3 { get; set; }
}
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();
};
}