473,748 Members | 2,528 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1373
On Mon, 23 Jul 2007 17:33:50 -0500, "TS" <ma**********@n ospam.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.mic rosoft.comwrote in message
news:7o******** ******@TK2MSFTN GHUB02.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
2095
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 -Blargedynsym -o python \ Modules/python.o \ libpython2.4.a -lresolv -lsocket -lnsl -ldl -lm
7
2216
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 file hard coded into the VBA like this. RunReg = Shell("regedit.exe /s F:\MyFolder\SQLfix.reg", 0) Now I wish to make this more generic and allow it to run from any path. I have tried to use CurrentProjectPath like this.
1
1858
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 that are https. I have to get rid of those, asap. I know HTML and beginning+ C#. I have heard that you do something in the web config? I guess that would be ideal. I only have about 10 pages that need the https links to be relative.
10
2847
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 running down all the subs or functions that may contain these values, I'd like one place to change them and have that changed reflected in the subs or functions that use those values. I'd like to avoid globals; keeping the values private to only those...
0
1613
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 seem to get it right. What I would like to see happen: The code opens the directory and loops through the files, opening them and processing them.
0
1103
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
2327
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 not have been hard coded* in SQL SERVER database to validate (each account was setup with the same generic password). Need to check each proc or function for presense of hard-coded password. We have hundreds of functions that may or may not...
1
2035
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 compile the project I get the following: Error 13 Unknown build error, 'The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.'
1
2710
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 where we used hard-coded passwords - Do you know a way of doing it automatically? If there is a free or not-free program that you know that can help it will be good. Thanks alot!
0
8995
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
8832
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,...
0
9561
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9332
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
8252
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6799
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
6078
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
4608
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
3316
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

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.