First Boolean example. We assign a Boolean variable to True or False. Then we use Booleans in a variety of ways in this simple program.
Part 1 We use the Not-operator to "invert" the value of the Boolean: change True to False and False to True.
Part 2 A Boolean can be used as an expression in an If-statement. We store the result of an expression variable.
Part 3 Boolean values and expressions can be used interchangeably. An If-statement can test a Boolean directly.
Part 4 You can assign an expression that evaluates to True or False directly to a Boolean variable as well.
Dim value As Boolean = True
' Part 1: flip the boolean.
value = Not value
' Part 2: this if-statement evaluates to false.
If (value) Then
' Part 3: evaluates to true.
If (Not value) Then
' Part 4: store expression result.
Dim result As Boolean = Not value And 1 = Integer.Parse("1")
Return Boolean. A Function can return True or False. We can even return the result of an expression that evaluates to True or False. Here in IsEmpty we return an expression's result.
Note If the List has a Length of 0, IsEmpty returns True. Otherwise False is returned.
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
' Use the IsEmpty boolean method.
' Add an element to the List and call IsEmpty again.
A review. We examined the Boolean type in VB.NET. With Boolean, you can store True and False and also the result of expressions that evaluate to True or False.
Notes, continued. Booleans are necessary for If and ElseIf statements, and careful use of them can make your programs clearer and faster.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Mar 21, 2022 (image).