473,404 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

short can't arithmetic with constant?

sd
short Total=11;
short Index=Total-1;

will occurs the following error:

error CS0266: Cannot implicitly convert type 'int' to 'short'. An
explicit conversion exists (are you missing a cast?)

and this is correct:

short Index=(short)(Total-1);

A bit ugly...

Nov 17 '05 #1
4 3245
sd,

11 and 1 are considered integers by the compiler. Trying to place an
integer in a short can result in a possible data loss, hence the compiler
warning.

You can also do this:

short Index = Total - ((short) 1);

And that should work.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"sd" <sd******@sina.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
short Total=11;
short Index=Total-1;

will occurs the following error:

error CS0266: Cannot implicitly convert type 'int' to 'short'. An
explicit conversion exists (are you missing a cast?)

and this is correct:

short Index=(short)(Total-1);

A bit ugly...

Nov 17 '05 #2
Ron
.... and that's exactly what you have to do. Actually using int would be more
efficient believe it or not.

"sd" wrote:
short Total=11;
short Index=Total-1;

will occurs the following error:

error CS0266: Cannot implicitly convert type 'int' to 'short'. An
explicit conversion exists (are you missing a cast?)

and this is correct:

short Index=(short)(Total-1);

A bit ugly...

Nov 17 '05 #3
>short Index = Total - ((short) 1);

And that should work.


Nope, the result of the subtraction is an int so you still need the
cast.
Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #4
sd
OK...

Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

99
by: Glen Herrmannsfeldt | last post by:
I was compiling a program written by someone else about six years ago, and widely distributed at the time. It also includes makefiles for many different systems, so I know it has been compiled...
34
by: Andy | last post by:
Hi, Are 1 through 4 defined behaviors in C? unsigned short i; unsigned long li; /* 32-bit wide */ 1. i = 65535 + 3; 2. i = 1 - 3; 3. li = (unsigned long)0xFFFFFFFF + 3; 4. li = 1...
29
by: JS | last post by:
I would like to print char 'd': main(){ char g; g = 'a'; g = 'b'; g = 'c'; g = 'd';
4
by: ad | last post by:
I have a custom function which accept short as parameter. for example: public void myFun(short myShor) {...} Now I have a integer (int i), I want to take as the aprameter of myFun, How can I...
8
by: Lionel | last post by:
Hi all, I'm facing a strang issue doing calculation on Double values. I read some thread on the newsgroups warning about type translations (eg Single to Double), but here I'm doing no conversion...
15
by: Steffen Loringer | last post by:
Hi, I'm using the following function to join 2 char (byte) into one short on a 32 bit X86 platform: unsigned short joinUnsigShort(unsigned char a,unsigned char b) { unsigned short val = 0;...
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
5
by: jerry | last post by:
I need to modify the code of a command-line tool. The source code starts out like this: int main(int argc, char *argv) { int ch, A, B ; while ((ch = getopt(argc, argv, "AB")) != -1)...
10
by: Why Tea | last post by:
I understood that the CPU registers determine the size of the machine. But what is the correct way to code an arithmetic operation to avoid wrap-around when the code is to be run on both 32-bit and...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.