Solved

Decrease Int16 variable with 1

Posted on 2010-09-22
8
635 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
 
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
C# Export DataGridView 4 39
Exception in Log4Net 1 19
How to Get total minutes between two hours 4 27
XML & .net 5 19
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now