2012-01-09 13 views
1

Possible Duplicate:
The type of the conditional expression can not be determined?使用に奇妙なエラーがありますか?オペレータ

は、私は現在、この文を書いた:

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

を、それはこのエラーがあります:私は?後にnullを使用することができますなぜ

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

を?上記のコードと等価である場合はifの声明なし?

+0

Rad_8.SelectedValueタイプとは何ですか? – CharlesB

答えて

8

nullは何の種類を持っていないため、コンパイラは、条件文の種類を推測することはできませんし、それが期待される戻り値を考慮していません。そしてbyte -types - 使用し

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

私はNULL可能タイプを返さないbyte.Parse(...)方法は、したがって、コンパイラはnull間の暗黙的な変換がないと言っているので、それがあると信じています。 nullの値を(byte?)でキャストして、そのタイプを明示的に指定してみてください。