VB.Net/Language Basics/nullable

Материал из VB Эксперт
Версия от 15:42, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Check if nullable is null

<source lang="vbnet">

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


 </source>


Main with Nullable (Of T)

<source lang="vbnet"> 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


 </source>