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

Convert textbox value to a short:

Hi, I have a textbox in an asp.net app and all I want to do is store
the value of the textbox in a short if there is a value in the text
box;

I came up with the following (Where txtID is a asp.net textbox
control)

short ID = (txtID.Text != string.Empty ? short.Parse(txtID.Text) : 0);

But I get the following error

"Type of conditional expression cannot be determined because 'short'
and 'int' implicitly convert to one another"

I have tried

short ID = (txtID.Text != string.Empty ? Convert.ToInt16(txtID.Text) :
0);

and

short ID = (txtID.Text != string.Empty ? ((short)txtID.Text) : 0);

But still get errors

Any help appreciated!

Thanks
Markus

Jul 14 '07 #1
2 12984
Ma*******@gmail.com wrote:
Hi, I have a textbox in an asp.net app and all I want to do is store
the value of the textbox in a short if there is a value in the text
box;

I came up with the following (Where txtID is a asp.net textbox
control)

short ID = (txtID.Text != string.Empty ? short.Parse(txtID.Text) : 0);

But I get the following error

"Type of conditional expression cannot be determined because 'short'
and 'int' implicitly convert to one another"

I have tried

short ID = (txtID.Text != string.Empty ? Convert.ToInt16(txtID.Text) :
0);

and

short ID = (txtID.Text != string.Empty ? ((short)txtID.Text) : 0);

But still get errors

Any help appreciated!

Thanks
Markus
It's not the short value that is the problem, it's the zero. A numeric
literal (without a period) is of the type int, unless you specify otherwise.

Cast the literal to short: (short)0

Also: it's more efficient to check the length of the string than to make
a string comparison.

--
Göran Andersson
_____
http://www.guffa.com
Jul 14 '07 #2
On Jul 15, 9:14 am, Göran Andersson <gu...@guffa.comwrote:
Markus...@gmail.com wrote:
Hi, I have a textbox in an asp.net app and all I want to do is store
the value of the textbox in a short if there is a value in the text
box;
I came up with the following (Where txtID is a asp.net textbox
control)
short ID = (txtID.Text != string.Empty ? short.Parse(txtID.Text) : 0);
But I get the following error
"Type of conditional expression cannot be determined because 'short'
and 'int' implicitly convert to one another"
I have tried
short ID = (txtID.Text != string.Empty ? Convert.ToInt16(txtID.Text) :
0);
and
short ID = (txtID.Text != string.Empty ? ((short)txtID.Text) : 0);
But still get errors
Any help appreciated!
Thanks
Markus

It's not the short value that is the problem, it's the zero. A numeric
literal (without a period) is of the type int, unless you specify otherwise.

Cast the literal to short: (short)0

Also: it's more efficient to check the length of the string than to make
a string comparison.

--
Göran Andersson
_____http://www.guffa.com- Hide quoted text -

- Show quoted text -
Thankyou Göran
Regards
Markus

Jul 15 '07 #3

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

Similar topics

8
by: Assimalyst | last post by:
Hi i have a value entered into an asp text box, procedureDateTxtBx.Text, thet has the format dd/MM/yyyy. I need to convert this into a format recognisable by SQL server in order to properly query...
3
by: Eric BOUXIROT | last post by:
hi, i must convert all of these eVC++ prototypes to use with VB.NET.... DLLEXPORT long F_BDO_MessageBoxOK(char *IN_title, char *IN_msg ); DLLEXPORT long F_BDO_MessageBoxOUINON(char *IN_title,...
9
by: Igor Okulist | last post by:
int func(void**); { short* p = NULL; func(&p); //<<< here } Could somebody remind me why is this not allowed ? error message: "cannot convert parameter from 'short **' to 'void **'"
5
by: manmit.walia | last post by:
Hello All, I am stuck on a conversion problem. I am trying to convert my application which is written in VB.NET to C# because the project I am working on currently is being written in C#. I tried...
65
by: kyle.tk | last post by:
I am trying to write a function to convert an ipv4 address that is held in the string char *ip to its long value equivalent. Here is what I have right now, but I can't seem to get it to work. ...
4
by: Edwin Knoppert | last post by:
In my code i use the text from a textbox and convert it to a double value. I was using Convert.ToDouble() but i'm used to convert comma to dot. This way i can assure the text is correct. However...
5
by: rbtmaxwell | last post by:
Hello, Not sure If I'm posting in the correct group here, so I appologize in advance if I selected the wrong one. I am trying to initialize a short with a hex value... sounds trivial right?...
3
by: mrajanikrishna | last post by:
Hi Friends, I am accepting a number from the user entered in a textbox. I want to assign to a variable in my code and assignt this to that variable. double num1 = (double)txtNum1.text; ...
28
by: Fore | last post by:
Hello I am looking for some effecient way to convert a 32 bit unsigned integer to a 16 bit signed integer. All I want is the lower 16 bits of the 32 bit unsigned integer , with bit 15 (0..15) to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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: 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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...

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.