Search code examples
.netvb.netdategenericsnullable

Null (?) Operator returns incorrect value when using generic type constructor. VB.NET


I am experiencing some weird behavior when using Generic Types and null operator. Why does obj2.CurrentDate return a date value that appears to be incorrect when using the ? (short hand). If I long hand the null operator (if) in the property then it returns the correct and expected value. I thought the ? was the equivalent to the if(expression, returnIfTrue, returnIfFalse).

Also, if the generic type constructor is removed, then the null operator works as expected. Why is this?

Return If(CurrentObject1 IsNot Nothing, CurrentObject1.MyDate, Nothing) -- Long Hand Returns correct value

Return CurrentObject1?.MyDate --Short Hande - Returns incorrect value

1st Console Application - With Generic Type Constructor, CurrentDate is not what is expected.

Module Module1
    Sub Main()
        Dim obj1 As New MyObject1 With {.MyDate = CDate("2017-11-13T14:25:00Z")}
        Dim obj2 As New MyObject2(Of MyObject1)(obj1)

        Console.WriteLine(obj1.MyDate)
        Console.WriteLine(obj2.CurrentDate)

        Console.ReadKey()
    End Sub
End Module

Public MustInherit Class MyBaseObject1
   Property MyDate As Date
End Class

Public Class MyObject1
    Inherits MyBaseObject1
End Class

Public Class MyObject2(Of MyObjectType As {MyBaseObject1, New})
    Public Sub New(obj As MyObjectType)
        m_CurrentObject1 = obj
    End Sub

    Private m_CurrentObject1 As MyObjectType = Nothing
    Public ReadOnly Property CurrentObject1 As MyObjectType
        Get
            Return m_CurrentObject1
        End Get
    End Property
    Public ReadOnly Property CurrentDate As Date?
        Get
            Return CurrentObject1?.MyDate
        End Get
    End Property
End Class

Output of Console Application 1

11/13/2017 8:25:00 AM

2/24/0010 4:56:53 AM

2nd Console Application - Without generic type constructor. Works as expected

Module Module1
    Sub Main()
        Dim obj1 As New MyObject1 With {.MyDate = CDate("2017-11-13T14:25:00Z")}
        Dim obj2 As New MyObject2(obj1)

        Console.WriteLine(obj1.MyDate)
        Console.WriteLine(obj2.CurrentDate)

        Console.ReadKey()
    End Sub
End Module

Public MustInherit Class MyBaseObject1
    Property MyDate As Date
End Class

Public Class MyObject1
    Inherits MyBaseObject1
End Class

Public Class MyObject2
    Public Sub New(obj As MyBaseObject1)
        m_CurrentObject1 = obj
    End Sub

    Private m_CurrentObject1 As MyBaseObject1 = Nothing
    Public ReadOnly Property CurrentObject1 As MyBaseObject1
        Get
            Return m_CurrentObject1
        End Get
    End Property
    Public ReadOnly Property CurrentDate As Date?
        Get
            Return CurrentObject1?.MyDate
        End Get
    End Property

End Class

Output of 2nd console application

11/13/2017 8:25:00 AM

11/13/2017 8:25:00 AM


Solution

  • Yes, this looks like a compiler bug. Will fix it shortly. In the meantime you can use the following workaround: DirectCast(CurrentObject1, MyBaseObject1)?.MyDate