473,395 Members | 1,647 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,395 software developers and data experts.

adding a hard coded number to a short creates a build error

TS
short x = 1;
//this has build error saying missing conversion
x = x + 2;

Why can't you just add this?

I guess the 1 is a literal that translates to an int32?

How would I accomplish this statement?
Jul 23 '07 #1
3 1361
On Mon, 23 Jul 2007 17:33:50 -0500, "TS" <ma**********@nospam.nospam>
wrote:
>short x = 1;
//this has build error saying missing conversion
x = x + 2;

Why can't you just add this?

I guess the 1 is a literal that translates to an int32?

How would I accomplish this statement?
x = (short)(x + 2);

IIRC the + operator returns an int unless either operand is a long,
float or double.

rossum

Jul 23 '07 #2
Hi TS,

Rossum has given the right answer.

You can declare and initialize a short variable like this example:

short x = 32767;

In the preceding declaration, the integer literal 32767 is implicitly
converted from int to short. If the integer literal does not fit into a
short storage location, a compilation error will occur.

The following assignment statement will produce a compilation error,
because the arithmetic expression on the right-hand side of the assignment
operator evaluates to int by default.

short z = x + y; // Error: no conversion from int to short

To fix this problem, use a cast:

short z = (short)(x + y); // OK: explicit conversion

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 24 '07 #3
TS
thanks all

"Linda Liu [MSFT]" <v-****@online.microsoft.comwrote in message
news:7o**************@TK2MSFTNGHUB02.phx.gbl...
Hi TS,

Rossum has given the right answer.

You can declare and initialize a short variable like this example:

short x = 32767;

In the preceding declaration, the integer literal 32767 is implicitly
converted from int to short. If the integer literal does not fit into a
short storage location, a compilation error will occur.

The following assignment statement will produce a compilation error,
because the arithmetic expression on the right-hand side of the assignment
operator evaluates to int by default.

short z = x + y; // Error: no conversion from int to short

To fix this problem, use a cast:

short z = (short)(x + y); // OK: explicit conversion

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.
>

Jul 24 '07 #4

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

Similar topics

1
by: | last post by:
Hello, I tried to build Python 2.4.1 on a Reliant Unix system. Just after the python executable program has been built, I get the following error: ==== begin make output === CC -W1...
7
by: dixie | last post by:
I have been running some code from a form's on open event to run regedit if a registry key does not exist in the registry. It has been working fine, but I have had the path to the registry fix...
1
by: Luis Esteban Valencia | last post by:
have a website that uses both http and https. I am able to change any hard coded http links to relative paths. No problem. But, I have several aspx and ascx pages that contain hard coded links...
10
by: Brett | last post by:
If I have many hard coded values such as file paths, file names, timeouts, etc, where is the best place to define them? Meaning, in the case something needs changing for example, rather than...
0
by: tjonsek | last post by:
I am working with directories in PHP for the first time. I have code that I've changed multiple times to try different things. I would think this is pretty standard fare so I'm not sure why I can't...
0
by: Lubdha Khandelwal | last post by:
I'm using VS2005 to compile a file that only contains a single large (100MB) hard-coded char array. It looks like: unsigned char test = { 0x50,0x4b,0x3,0x4,0x14,0x0,0x0,0x0,0x8,0x0,0x5c,0x6c,...
4
Lokean
by: Lokean | last post by:
The problem: Company was bought out and we are bringing everything into complience. Passwords are not secure and do not need to be.(required by software we are using) Old passwords *may or may...
1
by: eljainc | last post by:
Hello, I have a problem when working with a program that uses XAML in one of the modules. I copied some XAML code from another working project into an existing project, then when trying to...
1
by: nitzanO | last post by:
Hey, I have a very big project with thousands and thousands of code lines. Until now we have used hard-coded passwords and we wish to stop. The problem is how detect all the places in the code...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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...

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.