15

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

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
user1726655
  • 167
  • 1
  • 2
  • 9

4 Answers4

25

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;
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • I'm afraid to say this didn't work, this is the error message : `Cannot implicitly convert type 'bool?' to 'bool'. An explicit conversion exists (are you missing a cast?)` – user1726655 Oct 07 '12 at 12:21
  • 4
    @user1726655 Ah, but then your left-hand side does not allow `null`s, you need to change `null` for `false` (or `true` if sending `null` means the item must be checked, though I doubt it). I made an edit to the answer, please take a look. – Sergey Kalinichenko Oct 07 '12 at 12:24
3

Assuming the Checked property is of type nullable bool, I would probably do the following:

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

If it takes a bool (not-nullable) you can convert the null to false easily with:

decesCheckBox.Checked = (getKeyDecesCheckBox == null ? (bool?)null : (bool?)getKeyDecesCheckBox).GetValueOrDefault();
dbattaglia
  • 678
  • 4
  • 7
0

decesCheckBox.Checked is of type bool. As such you must feed it either false or true.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
0

Your '? :' operator has two possible incompatible return types: if the object is null, then it returns the value null, which can be cast to any nullable type. If the object is not null, then its return type is bool.

I don't know what type 'Checked' is, but I suspect that its type is 'bool'.

The problem here is that you can't cast null to the 'bool' type, and so you have to decide what type you want it to be in the case the object is null. If you wanted it to be false, you could write the statement as:

decesCheckBox.Checked = (getKeyDecesCheckBox as bool) ?? false;

The ?? Operator assigns the value 'false' in the case where the object is null, or cannot be converted to a bool.

Scott Earle
  • 660
  • 9
  • 21