# 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)
LVL 1
###### Who is Participating?

Commented:
Ok.

The reason Int in C# equates to an Int32, is because it is the most used variation of an integer.

You would only really need to use shorts (Int16) or longs (Int64) if you had a requirement to work with a database that specified them.  Otherwise, it doesn't really affect anything to simply use Int (Int32).
0

Commented:
In code "1" is an Int32 I believe.

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);
0

Commented:
By the way, short in C# = Int16 and int in c# = Int32.
0

Author Commented:
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);
0

Commented:
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;
0

Author Commented:
Still same error here... Hmmm :)
But, I changed the variable to int and that works good enought:

int test = 100;
test = test - 1;
0

Commented:
http://msdn.microsoft.com/en-us/library/ybs77ex4.aspx

According to the above link, the result of adding two shorts is an int. So use the following:

test = (short)(test - 1);
0

Author Commented:
Thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.