从不使用"Form1.answer"字段

Daw*_*ime 0 c# textbox winforms

我从词典中得到一个随机词:

var word = File.ReadAllLines(@"c:\\CTEMP\\Dictionary2.txt");
Run Code Online (Sandbox Code Playgroud)

并仅部分显示它以供玩家猜测使用:

hintTextBox.Text = GetPartialWord(word[new Random().Next(word.Length)]);
var answer = word[new Random().Next(word.Length)]; // answer = word from dictionary
Run Code Online (Sandbox Code Playgroud)

但是,我无法将用户输入的单词与字典中的单词进行比较.

我试过了 :

  private string answer;  //assign answer to word from dictionary

    private void button2_Click(object sender, EventArgs e)
    {

        if (answerTextBox.Text == answer)
{MessageBox.Show("You Guessed The Word !");
Run Code Online (Sandbox Code Playgroud)

但是我得到以下警告:

警告CS0169字段'Form1.answer'从不使用WindowsFormsApplication2

关于如何比较answerTextBox中输入的答案的任何想法?

Rom*_*och 5

问题出在这一行:

var answer = word[new Random().Next(word.Length)];
Run Code Online (Sandbox Code Playgroud)

在这里,您可以创建新变量,而不是使用类级别1.在if语句中,您将文本框的值与类级别变量进行比较.此外,您会收到警告,因为您从未将值分配给类级别变量,而是比较文本框的值.

该行应更改为:

this.answer = word[new Random().Next(word.Length)]; //or without "this."
Run Code Online (Sandbox Code Playgroud)