C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Finally: We store the result of an expression in a Boolean variable. This clarifies some logic.
VB.NET program that uses Boolean
Module Module1
Sub Main()
Dim value As Boolean = True
Console.WriteLine(value)
' Flip the boolean.
value = Not value
Console.WriteLine(value)
' This if-statement evaluates to false and thus isn't entered.
If (value) Then
Console.WriteLine("A")
End If
' Evaluates to true.
If (Not value) Then
Console.WriteLine("B")
End If
' Store expression result.
Dim result As Boolean = Not value And 1 = Integer.Parse("1")
Console.WriteLine(result)
End Sub
End Module
Output
True
False
B
True
Tip: You can assign an expression that evaluates to True or False directly to a Boolean variable as well.
Further: Storing a complex expression's result in a Boolean can be used as an optimization, as it will only be evaluated once.
Note: If the List has a Length of 0, IsEmpty returns True. Otherwise False is returned.
VB.NET program that uses Boolean return value
Module Module1
Dim _values As List(Of String) = New List(Of String)
Function IsEmpty() As Boolean
' Return a boolean value.
' ... This returns true if the Count is equal to 0.
' ... It returns false otherwise.
Return _values.Count = 0
End Function
Sub Main()
' Use the IsEmpty boolean method.
Console.WriteLine(IsEmpty())
' Add an element to the List and call IsEmpty again.
_values.Add("bird")
Console.WriteLine(IsEmpty())
End Sub
End Module
Output
True
False
Tip: Booleans are necessary for If and ElseIf statements, and careful use of them can make your programs clearer and faster.