webressurs
asked on
Decrease Int16 variable with 1
Why does I get the error "Cannot convert type int to short" when doing like this:
Int16 test = 100;
test = test - 1;
This works, but I need to know why the code above fails:
Int16 test = 100;
test--;
(test is 99)
Int16 test = 100;
test = test - 1;
This works, but I need to know why the code above fails:
Int16 test = 100;
test--;
(test is 99)
By the way, short in C# = Int16 and int in c# = Int32.
ASKER
Get the same error ("Cannot convert type int to short") on this:
Int16 test = 100;
Int16 test2 = 1;
test = test - test2;
...and this:
Int16 test = 100;
test = test - (Int16)1;
...and this:
Int16 test = 100;
test = test - Convert.ToInt16(1);
Int16 test = 100;
Int16 test2 = 1;
test = test - test2;
...and this:
Int16 test = 100;
test = test - (Int16)1;
...and this:
Int16 test = 100;
test = test - Convert.ToInt16(1);
Sorry, wasn't in front of a machine where I could test it ;-P
But, this should work:
short test 100;
test = test - (short)1;
But, this should work:
short test 100;
test = test - (short)1;
ASKER
Still same error here... Hmmm :)
But, I changed the variable to int and that works good enought:
int test = 100;
test = test - 1;
But, I changed the variable to int and that works good enought:
int test = 100;
test = test - 1;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you!
Try one of these:
Int16 test = 100;
Int16 test2 = 1;
test = test - test2;
Or
Int16 test 100;
test = test - (Int16)1;
or
Int16 test 100;
test = test - Convert.ToInt16(1);