Don't know how to get the user input from the textbox

357mag

Well-known member
Joined
Mar 31, 2023
Messages
58
Programming Experience
3-5
Okay I've got a program that determines whether an integer entered is odd or even. I have the code written but I don't know how to get the integer entered into code. I've tried many things but none make the compiler happy.

Here is what I have:

C#:
 private void buttonOK_Click(object sender, EventArgs e)
        {
            int x;

            x = Convert.ToInt32(textBoxEnterInteger.Text());

            if (x % 2 == 0)
                labelAnswer.Text = "The number is even";
            else
                labelAnswer.Text = "The number is odd";
        }

The line which has the Convert.ToInt32 thing I know is wrong. I just don't know what to write.
 
Your solution is not solution at all. The Text of a TextBox is already a string. If you expect to determine whether a number is odd or even then you actually need a number, not a string. The code you posted looks pretty close, except that Text is a property and not a method, so you should not have any parentheses after it because there's no method call.
 
Your solution is not solution at all. The Text of a TextBox is already a string. If you expect to determine whether a number is odd or even then you actually need a number, not a string. The code you posted looks pretty close, except that Text is a property and not a method, so you should not have any parentheses after it because there's no method call.

Post your suggestion please.
 
I already told you that the code you posted looks OK to me, other than the spurious parentheses. If you remove them, does it work? If not, please explain EXACTLY how and where the behaviour of the code differs from your expectations. That means using the debugger.
 
I already told you that the code you posted looks OK to me, other than the spurious parentheses. If you remove them, does it work? If not, please explain EXACTLY how and where the behaviour of the code differs from your expectations. That means using the debugger.

I've never used a debugger. Please remember I'm just a hobbyist doing this all on my own. My code does work with the string variable so the program is done.
 
x = Convert.ToInt32(textBoxEnterInteger.Text());
As said Text is a property, not method, so remove the (). textBoxEnterInteger.Text, not textBoxEnterInteger.Text()

Also, when taking input from user that can write anything and not just what you request it is better to use int.TryParse method, it will try to parse/convert the string as number without throwing exception if it can't be done.
 
I've never used a debugger. Please remember I'm just a hobbyist doing this all on my own.

Learning to use the debugger is an essential skill regardless of being a pro or an amateur. If only pros were using it, then Microsoft would have stripped away the debugger facilities from the community edition, Linux would not have gdb as a default tool with its developer packages.
 
C#:
        private void buttonOK_Click(object sender, EventArgs e)
        {
            labelAnswer.Text = "The number is " + ((int)enterIntegerNumericUpDown.Value) % 2 == 0 ? "even":"odd";

        }
 
I've never used a debugger. Please remember I'm just a hobbyist doing this all on my own. My code does work with the string variable so the program is done.

Being a hobbyist shouldn't mean settling for writing bad code that reinforces ideas that will cause you more grief in the future. If you learn how to do things properly now, things will go smoother in the future. That would involve stopping what you're doing and learning how to use the debugger and it would also involve doing as has been suggested here multiple times already. We want to help but we want to help you become a better developer, not to just get something any which way with bandages and chewing gum. If you show us what you have done then we can tell you why it's a bad idea. If you follow the advice already provided then either it will work as is or it won't and you can explain what happens and we can help you fix it. If you refuse to take advice when it's offered, there's every chance people will lose interest in providing it. Doing it the wrong way now just means you'll need more help later and you'll have to unlearn what you think you've learned.
 
Being a hobbyist shouldn't mean settling for writing bad code that reinforces ideas that will cause you more grief in the future. If you learn how to do things properly now, things will go smoother in the future. That would involve stopping what you're doing and learning how to use the debugger and it would also involve doing as has been suggested here multiple times already. We want to help but we want to help you become a better developer, not to just get something any which way with bandages and chewing gum. If you show us what you have done then we can tell you why it's a bad idea. If you follow the advice already provided then either it will work as is or it won't and you can explain what happens and we can help you fix it. If you refuse to take advice when it's offered, there's every chance people will lose interest in providing it. Doing it the wrong way now just means you'll need more help later and you'll have to unlearn what you think you've learned.

You don't get it. I'm just a hobbyist. A musician who just programs on the side as a hobby. I'm going to make mistakes. I live in a small economically depressed town. I seriously doubt anyone else in this town is even into programming. The school I went to when I took a course in C++ many years ago dumped all their programming courses due to lack of interest and enrollment.

And programming is tough stuff. I have a musical mind, not an engineering mind.

I'm not settling for bad code. I found another way to solve the problem and the program runs fine.

I also tried the way that was recommended on this forum. Both ways work. I was extremely close except the unneeded parentheses. I removed those and the code works.

When I look at all the programs I have written in Java, C++, Python, and C# I think I've done quite well for a guitar player. And some of these programs were my own idea.

And the only programming I've done up until now has been console programming, which I personally like doing a lot. This Windows GUI stuff is totally new to me.
 
I applaud your persistence and drive to learn. All that we are trying to tell you is that even you are doing this as a hobby, there is value in learning how things should be done, as well as learning some essential skills. Keep at it!
 
As a hobbyist hand tool woodworker, I'm currently where you are at in trying to learn how to do things the right way and the necessary skills. Trying to enjoy the process...
 
Back
Top Bottom