Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Decrease Int16 variable with 1

Posted on 2010-09-22
8
Medium Priority
?
656 Views
Last Modified: 2012-05-10
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
Comment
Question by:webressurs
  • 4
  • 3
8 Comments
 
LVL 21

Expert Comment

by:chapmanjw
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

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

Author Comment

by:webressurs
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 21

Expert Comment

by:chapmanjw
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

by:webressurs
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

by:
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

by:jhshukla
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

by:webressurs
ID: 33741704
Thank you!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question