1

I am trying to check the state of an object, if it is null then I need to return null, otherwise, I need to return a Guid or a Bool.

Portrait_Enabled = _SiteImagesModel_ImagesViewModel == null ? null : _SiteImagesModel_ImagesViewModel.Enabled,
Portrait_PrimaryImage = _SiteImagesModel_ImagesViewModel.PrimaryImage ?? null,
Portrait_RecordID = _SiteImagesModel_ImagesViewModel.RecordID ?? null,
Portrait_RelatedImage = _SiteImagesModel_ImagesViewModel.RelatedImage ?? null,

I have tried both the ?? and the ? : and I get a compiler error stating:

  • Type of conditional expression cannot be determined because there is no implicit conversion between <null> and bool
  • Operator ?? cannot be applied to operands of type bool and <null>
  • Operator ?? cannot be applied to operands of type Guid and <null>

Both the Guid and Bool values are Nullable.

xGeo
  • 2,149
  • 2
  • 18
  • 39
John Schultz
  • 672
  • 1
  • 10
  • 29
  • What are the types of the variables you are assigning to and of your object `_SiteImagesModel_ImagesViewModel`?.. – Gilad Green Oct 03 '17 at 07:10
  • 1
    What lines give the actual error because your error is quite clear and likely just a small mistake – EpicKip Oct 03 '17 at 07:11
  • 7
    `?? null` is pointless. The intention of the `??` operator is to provide a diffferent value **when the left operand is null**. "Use null if the value is null" is redundant, it's the functional equivalent of `myBool == true`. – Flater Oct 03 '17 at 07:11
  • You've everything explained in the exception content. `Portrait_Enabled = _SiteImagesModel_ImagesViewModel == null ? null : _SiteImagesModel_ImagesViewModel.Enabled` should be `Portrait_Enabled = _SiteImagesModel_ImagesViewModel == null ? null : (bool?)_SiteImagesModel_ImagesViewModel.Enabled` – mrogal.ski Oct 03 '17 at 07:11
  • Also please show the bigger picture - this is part of initializing a method with the object initializer - please show it – Gilad Green Oct 03 '17 at 07:12
  • Just use standard ternary operator with casting to proper `bool?` or `Guid` in this case, null-coalescing operator doesn't work if one of the operands are null (it must be another value, e.g. `false` or `Guid.NewGuid`). – Tetsuya Yamamoto Oct 03 '17 at 07:13
  • @TetsuyaYamamoto, Please forgive my ignorance, I've been out of the game for 60 days and a little rusty. Could you please give me an example of what you're talking about? – John Schultz Oct 03 '17 at 07:26
  • @JohnSchultz Can you explain what type `Portrait_PrimaryImage`, `Portrait_RecordID` & `Portrait_RelatedImage` each belongs to? I'm in middle of writing an explanation, just tell what data type those 3 variables have. – Tetsuya Yamamoto Oct 03 '17 at 07:27
  • @TetsuyaYamamoto, `Portrait_PrimaryImage` is a bool, `Portrait_RecordID` is a guid and `Portrait_RelatedImage` is a bool – John Schultz Oct 03 '17 at 07:29

1 Answers1

2

The MS Docs explanation about null-coalescing operator (note emphasized part):

A nullable type can represent a value from the type’s domain, or the value can be undefined (in which case the value is null). You can use the ?? operator’s syntactic expressiveness to return an appropriate value (the right hand operand) when the left operand has a nullible type whose value is null. If you try to assign a nullable value type to a non-nullable value type without using the ?? operator, you will generate a compile-time error. If you use a cast, and the nullable value type is currently undefined, an InvalidOperationException exception will be thrown.

Since the right-hand operand must return appropriate value of corresponding data type, it can't be set directly as null even the assigned variable has nullable data type (both operands must be have same data type).

Basically when you define a null-coalescing operator as this:

Portrait_PrimaryImage = _SiteImagesModel_ImagesViewModel.PrimaryImage ?? [DefaultValueIfNull];

It will translated as this:

Portrait_PrimaryImage = _SiteImagesModel_ImagesViewModel.PrimaryImage == null ? [DefaultValueIfNull] : _SiteImagesModel_ImagesViewModel.PrimaryImage;

Since both operands in ternary operator & null-coalescing operator must have same type, if you still want null value to be passed, you can cast the right-hand operand to proper type like this:

Portrait_PrimaryImage = _SiteImagesModel_ImagesViewModel.PrimaryImage ?? (bool?)null;

Portrait_RecordID = _SiteImagesModel_ImagesViewModel.RecordID ?? (Guid?)null;

Portrait_RelatedImage = _SiteImagesModel_ImagesViewModel.RelatedImage ?? (bool?)null;

Note that null is not at the same type as Nullable<bool>, you need to cast into bool? to get same data type.

As in your case, ternary operator usage just enough, no need to use null-coalescing operator when the second operand is null:

Portrait_Enabled = _SiteImagesModel_ImagesViewModel == null ? null : (bool?)_SiteImagesModel_ImagesViewModel.Enabled;

Portrait_RecordID = _SiteImagesModel_ImagesViewModel == null ? null : (Guid?)_SiteImagesModel_ImagesViewModel.RecordID;

Portrait_RelatedImage = _SiteImagesModel_ImagesViewModel == null ? null : (bool?)_SiteImagesModel_ImagesViewModel.RelatedImage;

References:

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

How to set null to a GUID property

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61