Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# : Implicit conversion between '<null>' and 'bool'

Tags:

c#

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

like image 738
user1726655 Avatar asked Oct 07 '12 12:10

user1726655


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C programming used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Why is C named so?

Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C".

What is C of computer?

C is a high-level and general-purpose programming language that is ideal for developing firmware or portable applications. Originally intended for writing system software, C was developed at Bell Labs by Dennis Ritchie for the Unix Operating System in the early 1970s.


1 Answers

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;
like image 60
Sergey Kalinichenko Avatar answered Oct 12 '22 16:10

Sergey Kalinichenko