Convert textbox value to a short:

M

MarkusJNZ

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
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

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.
 
M

MarkusJNZ

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top