C#的隐式转换'<&空GT;'和“布尔” [英] C# : Implicit conversion between '<null>' and 'bool'

查看:187
本文介绍了C#的隐式转换'<&空GT;'和“布尔”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个奇怪的错误消息,当我试图给对象转换为布尔,这里是我的代码:

 公共部分类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 to bool, 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> and bool

解决方案

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 to bool? makes it compatible with null of the conditional expression.

If the left-hand side of the assignment does not allow nulls, you need to decide on the value to set when getKeyDecesCheckBox is null. Usually, that's a false:

 decesCheckBox.Checked = getKeyDecesCheckBox == null ? false : (bool)getKeyDecesCheckBox;

这篇关于C#的隐式转换'&LT;&空GT;'和“布尔”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆