VB.Net/Language Basics/nullable
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
Main with Nullable (Of T)
Class MyClass1
Public numericValue As Nullable(Of Integer)
Public boolValue As Nullable(Of Boolean) = True
Public Function GetIntFromDatabase() As Nullable(Of Integer)
Return numericValue
End Function
Public Function GetBoolFromDatabase() As Nullable(Of Boolean)
Return boolValue
End Function
Public Shared Sub Main()
Dim dr As New MyClass1()
Dim i As Nullable(Of 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 Nullable(Of 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 Class