2

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?

GrayFur
  • 49
  • 1
  • 1
  • 11
  • Do you have any compiler or runtime errors you would like to share with us? – Dmitrii Dovgopolyi Jun 18 '17 at 18:23
  • There are no errors. Should there be any? – GrayFur Jun 18 '17 at 18:24
  • are you sure KeyPress gets executed? – adjan Jun 18 '17 at 18:30
  • Did yo try to step through your code with the debugger? Set a breakpoint in `Form1_KeyPress` and `button1_Click`, then step into the methods of the `Word` class. – M4N Jun 18 '17 at 18:31
  • Your code that you have posted should work as long as the `Form1_KeyPress` is triggered. So do you want to compose the word if the key is pressed anywhere on the form? Because it seems that is what you are doing. – CodingYoshi Jun 18 '17 at 18:41
  • First of all, **kudos to you** for taking [your old question](https://stackoverflow.com/questions/44608478/using-a-variable-in-multiple-functions-of-the-class-c-sharp) and making a nice, concise [mcve]. This is a huge improvement from your previous question. That said... _"As far as I know, the string variable "word" changes only in void function"_ -- given the code you posted, this statement is not correct. But, given the code you posted, things would work. Are you sure you've subscribed your event handlers to the events in question? – Peter Duniho Jun 18 '17 at 18:42
  • 1
    Okay, I tested your code, and sure enough, even with the event handler subscribed, the `KeyPress` event handler does not get called. Turns out, this is because your `Button` is stealing the key input. The `Form` can't receive it, because key input is focused elsewhere. See marked duplicate for what you need to do to fix it. – Peter Duniho Jun 18 '17 at 18:53
  • Thanks for solving the problem! Everything works as it's supposed to in this example code. – GrayFur Jun 18 '17 at 19:06

0 Answers0