Solved

Decrease Int16 variable with 1

Posted on 2010-09-22
8
651 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
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!

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

636 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