Check if nullable is null : nullable « Language Basics « VB.Net






Check if nullable is null

   


Option Explicit On
Option Strict On

Class MyClass1
    Public numericValue As Integer?
    Public boolValue As Boolean? = True
    Public Function GetIntFromDatabase() As Integer?
        Return numericValue
    End Function

    Public Function GetBoolFromDatabase() As Boolean?
        Return boolValue
    End Function
End Class

Module Program

    Sub Main()
        Dim dr As New MyClass1()
        Dim i As Integer? = dr.GetIntFromDatabase()
        If (i.HasValue) Then
            Console.WriteLine("Value of 'i' is: {0}", i.Value)
        Else
            Console.WriteLine("Value of 'i' is undefined.")
        End If
        Dim b As Boolean? = dr.GetBoolFromDatabase()
        If (b.HasValue) Then
            Console.WriteLine("Value of 'b' is: {0}", b.Value)
        Else
            Console.WriteLine("Value of 'b' is undefined.")
        End If
    End Sub
End Module

   
    
    
  








Related examples in the same category

1.Main with Nullable (Of T)
2.To test nullable type variables for Nothing values, use the Is operator or the IsNot operator.