C#的隐式转换'<&空GT;'和“布尔” [英] C# : Implicit conversion between '<null>' and 'bool'
问题描述
我得到一个奇怪的错误消息,当我试图给对象
转换为布尔
,这里是我的代码:
公共部分类ModifierAuteur:DevExpress.XtraEditors.XtraForm
{
公共ModifierAuteur(对象getKeyDecesCheckBox)
{
decesCheckBox.Checked = getKeyDecesCheckBox == NULL?空:(布尔)getKeyDecesCheckBox;
}
}
这是错误消息:
无法确定条件表达式的类型,因为那里是
℃之间的隐式转换;零>
和布尔
块引用>
解决方案假设分配是可能的,你需要转换为可空
布尔
,就像这样:decesCheckBox.Checked = getKeyDecesCheckBox == NULL?空:(?布尔)((布尔)getKeyDecesCheckBox);
内投地
布尔
unboxes值,外铸为布尔?
使得它与空
条件表达式的兼容。
如果转让的左侧不允许
空
S,你需要在价值决定设置时,getKeyDecesCheckBox
是空
。通常情况下,这是一个假
:decesCheckBox.Checked = getKeyDecesCheckBox ==空值 ?假:(布尔)getKeyDecesCheckBox;
I got a weird error message when I tried to convert an
object
tobool
, here is my code:public partial class ModifierAuteur : DevExpress.XtraEditors.XtraForm { public ModifierAuteur(object getKeyDecesCheckBox) { decesCheckBox.Checked = getKeyDecesCheckBox == null ? null : (bool)getKeyDecesCheckBox; } }
and this is the error message :
Type of conditional expression cannot be determined because there is no implicit conversion between
<null>
andbool
解决方案Assuming that the assignment is possible, you need to convert to a nullable
bool
, like this:decesCheckBox.Checked = getKeyDecesCheckBox == null ? null : (bool?)((bool)getKeyDecesCheckBox);
The inner cast to
bool
unboxes the value, and the outer cast tobool?
makes it compatible withnull
of the conditional expression.If the left-hand side of the assignment does not allow
null
s, you need to decide on the value to set whengetKeyDecesCheckBox
isnull
. Usually, that's afalse
:decesCheckBox.Checked = getKeyDecesCheckBox == null ? false : (bool)getKeyDecesCheckBox;
这篇关于C#的隐式转换'<&空GT;'和“布尔”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!