-1

Supose that exp.Row.IsdeliveryDateNull() returns True.
Having this code:

Dim theDate As Date?

If exp.Row.IsdeliveryDateNull() Then
    theDate = Nothing
Else
    theDate = exp.Row.deliveryDate
End If
' Result: theDate = Nothing

theDate = If(exp.Row.IsdeliveryDateNull(), Nothing, exp.Row.deliveryDate)
' Result: theDate = is #1/1/0001 12:00:00 AM# (Default value of Date)

Why does theDate gets different value depending on the type of if (normal or inline)?
I was expecting theDate = Nothing in both ways.

Similar question I found: Why C# inline if result is different than if?

sergidb
  • 25
  • 8
  • The code you have provided above looks ok to me. Please provide an [mcve] – Igor Sep 21 '18 at 14:55
  • Does it do what you want if you change `Nothing` in the inline version to `DirectCast(Nothing, Date?)`? Otherwise, please tell us what values you do get. – Andrew Morton Sep 21 '18 at 14:58
  • Sorry @Igor, I thought it would be clear. – sergidb Sep 21 '18 at 15:10
  • Thanks @AndrewMorton, with DirectCast it produces the result I want. – sergidb Sep 21 '18 at 15:10
  • 1
    [Déjà vu](https://stackoverflow.com/questions/52248491/ternary-if-exhibiting-odd-behavior-with-nullable-datetime-in-vb-net) – TnTinMn Sep 21 '18 at 17:27
  • @TnTinMn : Lately, for some reason, a lot of people have asked very similar questions to each other (all of which have already been answered many times before). Just in the past 2 weeks I've seen this happen _**at least twice**_ (4 questions, 2 similar, all duplicates). Either there are very many newcomers, or people have started spending less time researching, _**or**_ the search engines don't display as good results as they used to. – Visual Vincent Sep 21 '18 at 22:27
  • 1
    @VisualVincent, Add to that, people that one would hope would help enforce the site's duplicate policy, but instead answer the question. It gets even better when one of their previous answers could be linked to. – TnTinMn Sep 21 '18 at 22:48

1 Answers1

1

The If operator will never interpret Nothing as a nullable value type unless the other possible return type is a nullable value type as well. If the value is a regular value type then Nothing will always be interpreted as the default value of that type. For the return type of If to be Date? then at least one of the possible return values must actually be a Date? explicitly:

theDate = If(exp.Row.IsdeliveryDateNull(), Nothing, New Date?(exp.Row.deliveryDate))

or:

theDate = If(exp.Row.IsdeliveryDateNull(), DirectCast(Nothing, Date?), exp.Row.deliveryDate)
jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • Nice explanation @jmcilhinney. So if `exp.Row.deliveryDate` was of type `Date?` instead of `Date`, the inline if would produce a `Date?`? – sergidb Sep 21 '18 at 15:17
  • In theory, yes, but a property of a typed `DataRow` can never be a nullable value type. If it could then there'd be no need for those `IsXyzNull` methods. The whole issue results from the fact that ADO.NET originated in a time before nullable value types and so they chose to use a specific type - `DBNull` - to represent database NULLs. That was because, while `Nothing` could represent no value for reference types, it couldn't do so for value types. Now that it can, it's too late. Something like Entity Framework doesn't have this issue, although it still uses ADO.NET under the hood. – jmcilhinney Sep 22 '18 at 02:18