Tuesday, September 27, 2016

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

Problem

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. 

Solution

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


No comments:

Post a Comment