There's a class made to compose a word from symbols and return the word as a string. I've been struggling to make this work for hours and looks like I'm stuck.
The class Word.cs looks like this
namespace ReturningTheValueFromVoid
{
class Word
{
string word;
public void ComposeWord(char enteredSymbol)
{
word += enteredSymbol;
}
public string GetWord()
{
return word;
}
}
}
And the class where I need to get the word (on click of the button):
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);
}
}
}
So no matter how many symbols I add to my word, MessageBox returns nothing. As far as I know, the string variable "word" changes only in void function, so it can't be returned from another function. Is there a way to do this?