473,802 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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)(T otal-1);

A bit ugly...

Nov 17 '05 #1
4 3264
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.co m

"sd" <sd******@sina. com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.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)(T otal-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)(T otal-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
9105
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 with many different compilers. I got compile errors when it used va_arg to fetch an argument of type short. That seemed a little strange to me, so I changed it to int and it compiled just fine. So now I wonder, just what is the rule for va_arg...
34
16694
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 - 3;
29
22582
by: JS | last post by:
I would like to print char 'd': main(){ char g; g = 'a'; g = 'b'; g = 'c'; g = 'd';
4
30696
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 cast i to short ?
8
2617
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 at all. The issue I'm facing looks so basic but I'm really lost... I'm doing all my calculations in Double, and the results are not correct. Did I miss something?
15
8376
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; val = a; val <<= 8;
26
3067
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 read some of the items from the bottom up of the buffer, and some from the top down, moving the bottom items back to the new re-allocated bottom on every file read. Then when I've read all four files, I sort the top and bottom items separately...
5
3180
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) switch (ch) { case 'A':
10
4329
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 64-bit machines? Example: uint32_t x = SOME_LARGE_NO; uint64_t y; ....
0
9699
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9562
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10285
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10063
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7598
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6838
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2966
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.