Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

Decrease Int16 variable with 1

Posted on 2010-09-22
Medium Priority
654 Views
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)
0
Question by:webressurs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 4
• 3

LVL 21

Expert Comment

ID: 33738271
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

LVL 21

Expert Comment

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

LVL 1

Author Comment

ID: 33738396
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

LVL 21

Expert Comment

ID: 33738463
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

LVL 1

Author Comment

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

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

LVL 21

Accepted Solution

chapmanjw earned 1000 total points
ID: 33738676
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

LVL 9

Assisted Solution

jhshukla earned 1000 total points
ID: 33739299
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

LVL 1

Author Closing Comment

ID: 33741704
Thank you!
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month11 days, 6 hours left to enroll