logo  If, ElseIf, Iif and #If
This Chapter
If
Select
Chapters
Home Page
Colours, RGB
Computer Specifications
Dates&Times
Disk Drives
Files
Folders
GPS and OS Ref
VB.Net Forms
Image Files
If & Select
List/Array
Mathematics
NuGet
Sound
String Functions
Sun and Moon
User Controls
Validation
DigitalDan Sites
My Other Sites
Contact Site

Note
Some pages
may contain
inaccuracies
Hits=6
In these examples, I will assume you are already familiar with the normal structure of the If ... End If block

If i = 1 Then
 string1 = "One"
Else
 string1 = "Not One"
End If
  
I will therefore concentrate on other "If" variants.
 
ElseIf
The keyword "ElseIf" can be used within "If" ... "End If" blocks to make complex code easier to read. "ElseIf" is very similar to saying "Else If" but it does not need a closing "End If". In these examples both code blocks produce identical results

If i = 1 Then
 Return "One"
Else
 If i = 2 Then
  Return "Two"
 Else
  Return "Not one and not two"
 End If
End If
'
If i = 1 Then
 Return = "One"
ElseIf i = 2 Then
 Return "Two"
Else
 Return "Not one and not two"
End If
  
Although the benefit of "ElseIf" is not obvious in the above example, it can have advantages within complex "If" blocks.

If i = 1 Then
 Return = "One"
Else
 If i = 2 Then
  Return = "Two"
 Else
  If i = 3 Then
   Return = "Three"
  Else
   If i = 4 Then
    Return = "Four"
   Else
    If i = 5 Then 
     Return = "Five"
    Else
     Return = "Not 1, Not 2, Not 3, Not 4 and Not 5"
    End If
   End If
 End If
End If
'
If i = 1 Then
    Return1 = "One"
ElseIf i = 2 Then
    Return1 = "Two"
ElseIf i = 3 Then
    Return1 = "Three"
ElseIf i = 4 Then
    Return1 = "Four"
ElseIf i = 5 Then
    Return1 = "Five"
Else
    Return1 = "Not 1, Not 2, Not 3, Not 4 and Not 5"
End If
  
Abbreviated "If" and Iff
Some computer languages support an abbreviated version of their If statement. They often have a structure similar to this

i = 1?Return "One":Return "Not One";
  
If you are not familiar with "?" query strings, the code below produces identical results using a standard VB.Net If statement

If i = 1 Then
 Return "One"
Else
 Return "Not One"
End If
  
The VB.Net "If" keyword has a variant which is suitable for query strings, achieving a complete If ... Then ... Else in one line. This "If" keyword also returns a boolean value True/False indicating whether the "If" or the "Else" code was executed. The next example replicates the query string and also collects the boolean value in the variable tf.

Dim tf As Boolean = If(i = 1, return1 = "One", return1 = "Not One")
  
VB.Net can also mimic a query string without returning the boolean. This is achieved by replacing the word "If" (with one i) with a new keyword "Iif" (with two i's)

Iif(i = 1, return1 = "One", return1 = "Not One")
  
Just becuase "If" (as a query) and "Iif" exist,does not guarantee they are the best option. The query string example could be coded in a single line using a normal If syntax. (An approach that uses code which many VB.Net programmers find easier to understand.)

If i = 1 Then Return "One" Else Return "Not One"
  

 
#If
Watch out for the "#If" keyword when reading VB.Net code. "#If" is not a normal "If", it is a special instruction to the compiler. The "#If" block ends with "#End If". When your project is compiled, the computer decides whether the "#If" condition is true or false. When true, any code within the #If block will be included as part of the program. When false, the compiler will ignore any code in the #If block. The #If ... #End If structure supports #Else. Compiler directives are best suited to advanced programmers in specialist circumstances. It is unusual for a "Normal" programmer to encounter problems where "#If" provides the only viable solution.Here is an example of an "#If" block.

#If NET10_0_OR_GREATER Then
 ' include this code for recent .Net versions
#ELSE
 ' include this code for old .Net versions
#End If
  

DigitalDan.co.uk