1

Possible Duplicate:
The type of the conditional expression can not be determined?

I currently wrote this statement:

byte? Col_8 = (Rad_8.SelectedValue == null) ? null : byte.Parse(Rad_8.SelectedValue);

but it has this Error:

Type of conditional expression cannot be determined because there is no implicit conversion between '<null>' and 'byte'

why I can use null after ? ? what if equivalent of above code without if statement?

Community
  • 1
  • 1
Arian
  • 12,793
  • 66
  • 176
  • 300

3 Answers3

8

The compiler cannot infer the type of the conditional statement because null has no type and it does not consider the expected return value. Use

(Rad_8.SelectedValue == null) ? (byte?)null : byte.Parse(Rad_8.SelectedValue);
Jens
  • 25,229
  • 9
  • 75
  • 117
0
if(Rad_8.SelectedValue == null)
    Col_8 = null;
else 
    Col_8 = byte.Parse(Rad_8.SelectedValue);  
Haris Hasan
  • 29,856
  • 10
  • 92
  • 122
0

I believe it is because the method byte.Parse(...) does not return a nullable type, therefore the compiler is saying there is not implicit conversion between the null- and byte-types. Try casting the null value with (byte?) to explicitly specify it's type.

Samuel Slade
  • 8,405
  • 6
  • 33
  • 55