private void btnNext_Click(object sender, EventArgs e)
{
int i = 0;
nameTxtBox.Text = employee[i].name;
addTxtBox.Text = employee[i].address;
payTxtBox.Text = ($"{employee[i].CalcSalary():c}");
i++;
}
我尝试做的是每次单击“下一步”按钮时,为对象数组employee
中的每个对象显示适当的值。我怎么能这样做?
答案 0 :(得分:1)
您的代码几乎是正确的,但您在错误的范围内定义了i
变量。它必须是类中的一个字段,其方式是在每次按钮单击之间保持其先前值。
private int i = 0;
private void btnNext_Click(object sender, EventArgs e)
{
nameTxtBox.Text = employee[i].name;
addTxtBox.Text = employee[i].address;
payTxtBox.Text = ($"{employee[i].CalcSalary():c}");
i++;
// Add logic to make sure 'i' does not go higher than
// the total number of items in the array or IndexOutOfBoundException occurs.
}