0
votes

I'm getting the following error, when accessing the .HasValue property of a nullable DateTime? variable (when it doesn't have a value).

It works fine on my development machine (Win 10, VS 2017), but after being built by a TFS v15.117 build definition (set to use VS 2017 version), and published to a client's server (Windows Server 2012 R2 Standard), the following error is thrown:

[NullReferenceException: Object variable or With block variable not set.] Microsoft.VisualBasic.CompilerServices.Container..ctor(Object Instance) +1479606
Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack) +250

Why would myDateTimeVar.HasValue work on one system, but fail like this on another?

EDIT:

Dim testDate1 As DateTime? = Nothing
Dim testDate2 As DateTime? = DateTime.Now
Dim testDate3 As DateTime? = DateTime.MinValue
Dim testDate4 As DateTime?
Debug.WriteLine(testDate1.HasValue) 'False
Debug.WriteLine(testDate2.HasValue) 'True
Debug.WriteLine(testDate3.HasValue) 'True
Debug.WriteLine(testDate4.HasValue) 'False

[Based around discussion in comments on J's answer] This code runs perfectly in the project locally. Does Option Strict get applied differently in different Configurations / Environments do you think? (Although the TFS Build Def was set to use Debug)

1
Where's the code? Why are you using late-binding in the first place? You should set Option Strict On for this project and for every other project and leave it On unless you specifically need late-binding. Even if you do need it, you should create partial classes containing the minimum possible code that uses late-binding and turn Option Strict Off just for those files.jmcilhinney
Heh, you've spotted the dirty secret at the heart of the sprawling legacy system I'm working on, Option Strict is very much Off I hadn't thought about turning it on, on a class by class basis, where possible. I'll give that a go. I'll put a code sample up too in a sec.Ted
All the examples you posted are early-bound. Try assigning all those DateTime? variables to Object variable and get HasValue on those, as I demonstrated in my answer. I'll wager that you'll see the same behaviour I did.jmcilhinney

1 Answers

1
votes

I just tested this code and saw the same behaviour you describe:

Option Strict Off

Module Module1

    Sub Main()
        Dim nullableDate As Date? = Nothing
        Dim boxedNullableDate As Object = nullableDate

        Console.WriteLine(boxedNullableDate.HasValue)
        Console.ReadLine()
    End Sub

End Module

The reason for the exception would be that boxing a Date? that has no value gives you an Object reference that is Nothing and trying to access any member of Nothing throws a NullReferenceException.

That basically means that late-binding with nullable value types is just not going to work.

EDIT:

Interestingly, I just changed the code to this:

Option Strict Off

Module Module1

    Sub Main()
        Dim nullableDate As Date? = Date.Now
        Dim boxedNullableDate As Object = nullableDate

        Console.WriteLine(boxedNullableDate.HasValue)
        Console.ReadLine()
    End Sub

End Module

and now I get a MissingMemberException with the message:

Public member 'HasValue' on type 'Date' not found.

It seems like boxing a nullable value type doesn't retain the knowledge that the original variable was nullable. The debugger just recognises the Object variable as Object alone if it's Nothing, otherwise Object {Date}.