Most of times we use variables to store the response from the user; might be a simple message box. In that case, we would be using a boolean variable - one that can store True or False. What if the User didn't answer the question? It would be taken as False by default. To overcome this you can use the variable as Nullable. Now the var can have three states - True , False and Null
Sub Nullable_Example()
' Nullable type will be used to check if the value is assigned
Dim Answer As Nullable(Of Boolean)
Dim RetVal ' As MsgBoxResult
RetVal = MsgBox("Have you booked the ticket for olympics", vbYesNoCancel, "DotNetDud Examples")
If RetVal = vbYes Then
Answer = True
ElseIf RetVal = vbNo Then
Answer = False
End If
'Gets a value indicating whether the current Nullable<(Of <(T>)>) object has a value.
If Answer.HasValue Then
MsgBox("Question answered")
Else
MsgBox("Question not answered")
End If
' The above checks if the question is answered or not. Here the boolean variable answer can store True, False and Other
End Sub
No comments:
Post a Comment