I was having trouble with a nullable type so I wrote the following program to demonstrate the issue I was having and am confused by the results. Here is the program:
Module Module1
Public Sub Main()
Dim i As Integer? = Nothing
Dim j As Integer? = GetNothing()
Dim k As Integer? = GetNothingString()
If i.HasValue Then
System.Console.WriteLine(String.Format("i has a value of {0}", i))
End If
If j.HasValue Then
System.Console.WriteLine(String.Format("j has a value of {0}", j))
End If
If k.HasValue Then
System.Console.WriteLine(String.Format("k has a value of {0}", k))
End If
System.Console.ReadKey()
End Sub
Public Function GetNothingString() As String
Return Nothing
End Function
Public Function GetNothing() As Object
Return Nothing
End Function
End Module
The output of the program is:
k has a value of 0
Why does only k have a value?
It has to do with the implicit conversion of a string to an integer.
The others are set straight to Nothing or have Nothing as an Object sent to it, which doesn’t have an implicit conversion. String, however, does.
Try it again with Option Strict turned on. I bet none print.