Booleans inside Functions

Booleans are a type of variable used for checking if a condition is True or False. For example, say you want to find out if a number is odd or even. You create a function with a boolean variable and have the code perform a simple calculation on the number. Then output the results to "true" or "false". An "If/Else" statement is often used to check the status of a boolean. It works like an "off/on" switch. On is yes or true and Off is no or false - depending on how you want to "read" your code. Below is example code in both VB.NET and C# using a boolean followed by a simple interactive example web page programmed using a boolean.

Boolean used to check True/False status:

This code will check to see if a number is odd or even. The variable "IsEven" is set to either true or false based on whether or not the number can be divided by 2.

VB.NET CODE using a boolean:


Function IsEven(lngNum As Long) As Boolean
   ' Determines whether a number is even or odd.
   
   If lngNum Mod 2 = 0 Then
      IsEven = True
   Else
      IsEven = False
   End If
End Function
 

Below is the exact web page programmed in C# instead of VB.NET.

C# CODE using a boolean:


public bool IsEven(long lngNum) 
{ 
    bool functionReturnValue = false; 
    // Determines whether a number is even or odd. 
    
    if (lngNum % 2 == 0) { 
        functionReturnValue = true; 
    } 
    else { 
        functionReturnValue = false; 
    } 
    return functionReturnValue; 
} 

 

VB.NET vs. C#

Variables

Decisions

Built In Functions