If Operator and Nothing on VB.Net

I encountered an interesting thing recently in my programming experience. I have the following method:
Private Sub SomeMethod(parameter As Nullable(Of Double))
   If parameter.HasValue Then
      'Do something
   Else
      'Do something else
   End If
End Sub
To call it, I use the following:
SomeMethod(If(someObject.IsTrue, Nothing, someDouble))
However, it didn't work as I expect it to be. The one that confused me is, the HasValue always returns true although Nothing is being passed to the method. 

It took me awhile to realize that the If operator in this case will return the same type for both results (true and if false). And in VB.Net, Nothing equals default value which for Double is 0.0 while I want Nothing to act as Null. Thus to fix the issue, I have to rewrite it as:

If someObject.IsTrue Then
   SomeMethod(Nothing)
Else
   SomeMethod(someDouble)
End If


Comments

Popular posts from this blog

AWS EC2 Can't Reach EC2 Metadata Service After Subnet Change

A2 Hosting with .NET Core 2.1

Xcode CodeSign Incorrectly States Password is Incorrect