Solved

Decrease Int16 variable with 1

Posted on 2010-09-22
8
645 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
[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
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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 250 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 250 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 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