i have a numerical textbox which I need to add it's value to another number I have tried this code
String add = (mytextbox.Text + 2)
but it add the number two as another character like if the value of my text box is 13 the result will become 132
if you want to make sure the conversion doesn't throw any exception
You can use the
int.Parse
method to parse the text content into an integer:Others have posted the most common answers, but just to give you an alternative, you could use a property to retrieve the integer value of the TextBox.
This might be a good approach if you need to reuse the integer several times:
And then you can use the property like this:
You need to convert the text to an integer to do the calculation.
The type of
mytextbox.Text
is string. You need to parse it as a number in order to perform integer arithmetic, e.g.Note that you may wish to use
int.TryParse
in order to handle the situation where the contents of the text box is not an integer, without having to catch an exception. For example:I prefer TryPase, then you know the fallback is going to be zero (or whatever you have defined as the default for intValue)