有一个类用于从符号组成单词并将单词作为字符串返回。我一直在努力让这项工作工作几个小时,看起来我被卡住了。
Word.cs类看起来像这样
namespace ReturningTheValueFromVoid
{
class Word
{
string word;
public void ComposeWord(char enteredSymbol)
{
word += enteredSymbol;
}
public string GetWord()
{
return word;
}
}
}
我需要获得单词的课程(点击按钮):
namespace ReturningTheValueFromVoid
{
public partial class Form1 : Form
{
Word word = new Word();
string stringWord;
public Form1()
{
InitializeComponent();
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
char pressedKey = e.KeyChar;
word.ComposeWord(pressedKey);
}
private void button1_Click(object sender, EventArgs e)
{
stringWord = word.GetWord();
MessageBox.Show(stringWord);
}
}
}
所以无论我添加多少符号,MessageBox都不会返回任何内容。据我所知,字符串变量“word”仅在void函数中更改,因此无法从其他函数返回。有没有办法做到这一点?