Home
Search
Boolean (True, False and Not)Use the Boolean type to represent a True or False value. Test Booleans in If-statements.
VB.NET
This page was last reviewed on Mar 21, 2022.
Boolean. A Boolean stores a value indicating True or False. It can be used as an expression in an If-statement. It can also store the result of an expression.
Usage notes. Booleans are used throughout VB.NET programs. We can return a Boolean from a function, or pass a Boolean as an argument.
If Then
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.
Module Module1 Sub Main() Dim value As Boolean = True Console.WriteLine(value) ' Part 1: flip the boolean. value = Not value Console.WriteLine(value) ' Part 2: this if-statement evaluates to false. If (value) Then Console.WriteLine("A") End If ' Part 3: evaluates to true. If (Not value) Then Console.WriteLine("B") End If ' Part 4: store expression result. Dim result As Boolean = Not value And 1 = Integer.Parse("1") Console.WriteLine(result) End Sub End Module
True False B True
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.
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
True False
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).
Home
Changes
© 2007-2023 Sam Allen.