Reputation: 4964
I have a little problem to make a simple math calculation in the controller.
what I try to do is add +1 to a number of a variable.
Here is an example for you to understand better what I try to do:
var a= formcollection["Id_this"];
var next = a + 1;
Note: the value of "Id_this
" is "1"
.
The result I need for the variable next
is 2
My problem is that the result of the variable next
is "12"
.
Upvotes: 1
Views: 1307
Reputation: 33809
Reason is you are doing string concatenation. Try this safe approach:
int number;
int next = 0;
if(Int32.TryParse(formcollection["Id_this"], out number))
{
next = number + 1;
}
else
{
//formcollection["Id_this"] is not a number
}
Upvotes: 1
Reputation: 174299
a
is a string
. Adding a number to a string results in the number being converted to a string and being concatenated.
To make it work, you first need to convert a
to a number:
var next = Convert.ToInt32(a) + 1;
Upvotes: 4