C# 从列表中输出值<&燃气轮机;
我想从C# 从列表中输出值<&燃气轮机;,c#,C#,我想从studentsList中输出每个student.FirstName,方法是按btnNext\u单击,从0值开始到最后,单击“下一步按钮”。 我试图这么做,但不知道如何从publicstaticlist studentsList=newlist()输出正确的值 使用变量存储学生的索引,并在每次按“下一步”时将其增加一次,这样每次都会得到下一个学生的名字 例如: int studentIndex; public void Next() { if (studentIndex >
studentsList
中输出每个student.FirstName
,方法是按btnNext\u单击
,从0值开始到最后,单击“下一步按钮”。
我试图这么做,但不知道如何从publicstaticlist studentsList=newlist()输出正确的值代码>
使用变量存储学生的索引,并在每次按“下一步”时将其增加一次,这样每次都会得到下一个学生的名字
例如:
int studentIndex;
public void Next()
{
if (studentIndex >= Student.studentsList.Count - 1)
studentIndex = 0;
txtFirstName.Text = Student.studentsList[studentIndex].FirstName;
studentIndex++;
}
到达最后一名学生后,它将重置为第一名。使用变量存储该学生的索引,并在每次按“下一步”时将其增加一次,这样每次都将获得下一名学生的姓名
例如:
int studentIndex;
public void Next()
{
if (studentIndex >= Student.studentsList.Count - 1)
studentIndex = 0;
txtFirstName.Text = Student.studentsList[studentIndex].FirstName;
studentIndex++;
}
到达最后一个学员后,它将重置为第一个。您需要将当前状态保留在程序中,以便实现像那样的“步进”。不过,您只需要当前索引:在构造函数中初始化它,然后在Next
方法中使用它。处理完当前索引后,将其递增,以便下次调用next
方法时获得后续值
int currentStudent = 0;
public void Clear() {
txtFirstName.Clear();
txtLastName.Clear();
txtCity.Clear();
currentStudent = 0;
}
public void Next() {
// You need to stop when you reach the end of the list
if (currentStudent < Student.studentsList.Count) {
txtFirstName.Text = Student.studentsList[currentStudent].FirstName;
currentStudent++;
}
}
int currentStudent=0;
公共空间清除(){
txtFirstName.Clear();
txtLastName.Clear();
txtCity.Clear();
currentStudent=0;
}
下一个公共空间(){
//当你到达列表的末尾时,你需要停止
if(currentStudent
为了实现这样的“步进”,您需要在程序中保持当前状态。不过,您只需要当前索引:在构造函数中初始化它,然后在Next
方法中使用它。处理完当前索引后,将其递增,以便下次调用next
方法时获得后续值
int currentStudent = 0;
public void Clear() {
txtFirstName.Clear();
txtLastName.Clear();
txtCity.Clear();
currentStudent = 0;
}
public void Next() {
// You need to stop when you reach the end of the list
if (currentStudent < Student.studentsList.Count) {
txtFirstName.Text = Student.studentsList[currentStudent].FirstName;
currentStudent++;
}
}
int currentStudent=0;
公共空间清除(){
txtFirstName.Clear();
txtLastName.Clear();
txtCity.Clear();
currentStudent=0;
}
下一个公共空间(){
//当你到达列表的末尾时,你需要停止
if(currentStudent
尝试将上次读取的索引存储为类成员
int lastIndexRead = 0;
public void Next()
{
if(lastIndexRead == Student.studentsList.Count) // do something - index is out of bounds of the array
else
{
txtFirstName.Text = Student.studentsList[lastIndexRead].FirstName;
lastIndexRead++;
}
}
尝试将上次读取的索引存储为类成员
int lastIndexRead = 0;
public void Next()
{
if(lastIndexRead == Student.studentsList.Count) // do something - index is out of bounds of the array
else
{
txtFirstName.Text = Student.studentsList[lastIndexRead].FirstName;
lastIndexRead++;
}
}
假设你想让按钮在每次单击时获取下一个可用的学生,我会使用一个队列,并在每次调用下一个时保持退出队列将枚举器取出,共享并重用它。。。无论如何,这种方法有点脆弱…只是想澄清一下,你是想一次显示一个学生(可以更改)还是所有学生?@ryanyyu从0开始到最后,点击“下一步按钮”,所以我想,点击“下一步”应该会给下一个学生。。。一个接一个..@AndreasNiedermair是的,我想用-btnCreateStudent\单击添加它们,然后逐个只输出每个的名字。在当前步骤中,我只从列表中获取最后一个值。假设每次单击时都要使按钮获取下一个可用的学生,我将使用队列,并在每次调用下一个时保持退出队列。将枚举数导出,共享并重用它。。。无论如何,这种方法有点脆弱…只是想澄清一下,你是想一次显示一个学生(可以更改)还是所有学生?@ryanyyu从0开始到最后,点击“下一步按钮”,所以我想,点击“下一步”应该会给下一个学生。。。一个接一个..@AndreasNiedermair是的,我想用-btnCreateStudent\单击添加它们,然后逐个只输出每个的名字。在当前步骤中,我只从列表中获取最后一个值。