Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

Basic calculate two numbers into one

Got a small question if someone can answer
i'm trying to make a new label that will calculate the numbers in 1 label + the value i want

Code
label31.Text = label29.Text + 15;
So let's say label29 is "2" i want the label31 to be 2+15 which is 17
but instead its just adding the number to the end of it like that "215"
labels are type string, when you call your current code as it is, it will treat the whole thing as a string combination instead of a mathematical operation.

a simple way to fix this is by converting the label which contains the number to integer then converting the output to string (if you want to display it)

Code
label31.Text = (Convert.ToInt32( label29.Text )+ 15).ToString() ;
(May 6, 2019 04:09 AM)Vexallus Wrote:  labels are type string, when you call your current code as it is, it will treat the whole thing as a string combination instead of a mathematical operation.

a simple way to fix this is by converting the label which contains the number to integer then converting the output to string (if you want to display it)

Code
label31.Text = (Convert.ToInt32( label29.Text )+ 15).ToString() ;

It gives a error while trying to start the Form
Code
System.FormatException: 'Input string was not in a correct format.'

Basically the label29 is a PlayerY coordinate in a game
and i'm trying to make label31 to calculate the playerY coordinate + the value i want

//Edit: solved it by using " label31.Text = (Convert.ToDecimal(label29.Text) + 15).ToString();"
thanks


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by littleblueguy24Jun 14, 2022 12:03 AM 2 122 Jun 14, 2022 12:03 AM
Last Post: littleblueguy24
[Java] by cryptobassboostFeb 7, 2019 05:28 PM 4 219 Feb 7, 2019 05:28 PM
Last Post: Caliban
[C#] by Carlsson94Dec 24, 2018 06:04 AM 4 202 Dec 24, 2018 06:04 AM
Last Post: Carlsson94