c# - 如何在按下按钮时使用不同的列表?

标签 c#

我目前正在做一个关于拼写的小测验。 该程序显示一个问题和 4 个可能的答案。 现在我遇到的问题是我不知道如何更改正在使用的列表。 我有 2 个问题列表:StamQuestions 和 StateQuestions。

 public void GiveAnswerA(object sender, EventArgs e)
 {
     if (TextAnswerA.Text == StamQuestions[numberQuestion].CorrectAnswer)
     {
         SetUI();
         score++;
     }
 }

 private void GiveAnswerB(object sender, EventArgs e)
 {
     if (TextAnswerB.Text == StamQuestions[numberQuestion].CorrectAnswer)
     {
         SetUI();
         score++;
     }
 }

 private void GiveAnswerC(object sender, EventArgs e)
 {
     if (TextAnswerC.Text == StamQuestions[numberQuestion].CorrectAnswer)
     {
         SetUI();
         score++;
     }
 }

 private void GiveAnswerD(object sender, EventArgs e)
 {
     if (TextAnswerD.Text == StamQuestions[numberQuestion].CorrectAnswer)
     {
         SetUI();
         score++;
     }
 }

 private void SetUI()
 {
     numberQuestion = rnd.Next(0, StamQuestions.Count);

     CurrentQuestion.Text = StamQuestions[numberQuestion].QuestionText;
     TextAnswerA.Text = StamQuestions[numberQuestion].Answers[0];
     TextAnswerB.Text = StamQuestions[numberQuestion].Answers[1];
     TextAnswerC.Text = StamQuestions[numberQuestion].Answers[2];
     TextAnswerD.Text = StamQuestions[numberQuestion].Answers[3];

     ResultAnswer.Text = "Punten : "+score.ToString();
 }

现在我想要发生的是,当我按下按钮时,StamQuestions 被 StateQuestions 取代。

如有任何帮助,我们将不胜感激!

编辑:这是列表的定义

        List<Question> StamQuestions = new List<Question>();
        List<Question> StateQuestion = new List<Question>();

最佳答案

一般来说,您只需存储对当前使用的问题列表的引用,并根据需要交换该引用。

currentQuestions添加另一个类级变量:

private List<Question> stamQuestions = new List<Question>(); 
private List<Question> stateQuestions = new List<Question>();
private List<Question> currentQuestions = stamQuestions;

在任何地方使用currentQuestions,例如:

public void GiveAnswerA(object sender, EventArgs e)
{
    if (TextAnswerA.Text == currentQuestions[numberQuestion].CorrectAnswer)
    {
        SetUI();
        score++;
    }
}

然后根据需要交换到另一组,我怀疑在 SetUI 中:

private void SetUI()
{
    currentQuestions = stateQuestions;

    numberQuestion = rnd.Next(0, currentQuestions.Count);
    CurrentQuestion.Text = currentQuestions[numberQuestion].QuestionText;
    TextAnswerA.Text = currentQuestions[numberQuestion].Answers[0];
    TextAnswerB.Text = currentQuestions[numberQuestion].Answers[1];
    TextAnswerC.Text = currentQuestions[numberQuestion].Answers[2];
    TextAnswerD.Text = currentQuestions[numberQuestion].Answers[3];
    ResultAnswer.Text = "Punten : "+score.ToString();
}

或者您可能打算在列表之间翻转/翻转,在这种情况下您也可以这样做:

currentQuestions = (currentQuestions == stamQuestions) ? stateQuestions : stamQuestions;

关于c# - 如何在按下按钮时使用不同的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59610275/

相关文章:

c# - 有没有办法在抛出 CLR 异常时触发事件

c# - 无法连接到任何指定的 mysql C#

c# - while循环中的HttpClient只执行一次

c# - Entity Framework : Issue with IDENTITY_INSERT - "Cannot insert explicit value for identity column in table"

c# - 在 Qt 项目中使用 PICkitS.dll

c# - 单击表单上任何数据 GridView 的保存按钮时提交当前脏单元格

c# - 如何让 Lucene (.NET) 使用通配符正确突出显示?

c# - Bootstrap 在 ASP.NET 中使用 gridview 分页保存当前选项卡

c# - 是否对 Contract.Ensures 执行静态检查?

c# - 在 ASP.Net 中包装 session 状态的首选方法是什么?