Convert textbox value to a short:

  • Thread starter Thread starter MarkusJNZ
  • Start date Start date
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
 
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.
 
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
 
Back
Top