jeudi 7 février 2019

If operator returns different value than specified, while the same If statement doesn't [duplicate]

This question already has an answer here:

I've run into the issue that my If operator returns an unexpected value and I don't understand why. Using seemingly the same piece of code with the If statement it does work as expected. An example of the code:

Dim c As Object = DBNull.Value

Dim d As Date?
If TypeOf c Is Date Then
    d = DirectCast(c, Date)
Else
    d = Nothing
End If
'd returns null as expected

Dim e As Date? = If(TypeOf c Is Date, DirectCast(c, Date), Nothing)
'e returns DateTime.MinValue

Aucun commentaire:

Enregistrer un commentaire