Darryl Darryl - 1 year ago 54
Vb.net Question

How can I add an Integer to a string and get a string answer?

I am using Visual Basic 2010.
I have tried several ways to try and add an integer to a string and then get the results as a string. Nothing I have tried seems to work. Below is the code I have at present time. Result is a label that I need to show the results after adding a 1 to the label result to get a new total. Can anyone help? Thanks!

Result.Text = CStr(CInt(Result.Text + 1))

Answer Source

You can not add numbers with a string. You need to first convert the string into an integer. You can do this like this:

Dim resultInteger = CInt(Result.Text)

You can then add resultInteger to 1 and set Result.text to the answer like this:

Result.Text = (resultInteger + 1).ToString

It, however, is possible to do this all on one line:

Result.Text = (CInt(Result.Text) + 1).ToString

Note that if Result.Text does not contain a number then it will crash you application. You could use a try statement like this:

Result.Text = (CInt(Result.Text) + 1).ToString
Catch ex As Exception
msgbox("Result does not contain an integer!")
End Try
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download