1

I currently have this, but it keeps resulting in the number say I put in 5 it will make it 51 instead of the result I want of 6. Can anyone help me?

int number;
int outcome;

number = int.Parse(numberInputTextBox.Text);

outcomeLabel.Text = number + 1 .ToString();

3 Answers 3

3
number = int.Parse(numberInputTextBox.Text);
outcomeLabel.Text = (number + 1).ToString();

You forgot to add ( ). Your sample was:

1) take 1 and convert to string
2) add number and string

in point 2) the number was casted to string before adding to second string. That's why you got string concatenation "5"+"1"="51" instead of integer sum 5+1=6

Sign up to request clarification or add additional context in comments.

Comments

1

1.ToString() will return a string, which you are then adding the string "5" to, as C# will implicitly cast the number 5 to the string "5" when trying to add it to a string.

You need to first add one, then convert to a string, giving something like this:

outcomeLabel.Text = (number + 1).ToString();

or

int newNumber = number + 1;
outcomeLabel.Text = newNumber.ToString();

Comments

0

Just add parentheses...

number = int.Parse(numberInputTextBox.Text);

outcomeLabel.Text = (number + 1).ToString();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.