Here’s How to Do an Inline IF statement in VB.NET
Contents
What is an “IF” Statement?
As you may already be aware, the “IF” statement is a conditional statement in any coding language, which usually is wrapped by the “IF, THEN” and “END IF” keywords/statements. The code between these two lines gets executed if the condition in the first “IF” statement is met, i.e., return value=true.
For example, in VB.Net:
Let’s say the value of “a” is 5.
Then I want a piece of code to be executed if “a” is less than 10. In that case, here is what my conditional code block would look like.
Public Module Program Public Sub Main(args() As string) Dim a as integer a=5 If (a<10) Then Console.WriteLine("A is less than 10") Else Console.WriteLine(“A is greater than or equal to 10”) End If End Sub End Module
When the control executes the “IF” statement, the return value of the condition is true because 5 is less than 10. Then the control flows into the block to execute the code inside (in this case, it is only the message box). Finally, the “END IF” statement marks the end of this execution.
Inline If Statement – IIF:
Here is a code block with the entire IF and END IF statement to display some message based on a condition.
The same code can be written in a single line using the inline IF statement–“IIF” statement. It will not require an “END IF” statement as the entire code is within a single line that marks both the beginning and the end of the if block.
In this example, both the condition and the code block are managed within a single line of code; hence, this is called an “Inline If” condition.
Syntax:
IIf( < condition > , < True statement > , < false statement > )
The Same Block of Code Using IIF:
Public Module Program Public Sub Main(args() As string) Dim a as integer a=5 Console.WriteLine(IIf (a<10,"A is less than 10","A is greater or equal to 10")) End Sub End Module
Now, we try the same code using a different value for “a.” Here’s the code and the output:
More About the IIF Statement:
1. The IIF statement is a built-in function available only with the VB language within .NET. It also returns something. You should notice that in the above example that the IIF statement is wrapped inside the “Console.Writeline” rather than the Console statement being a part of the true/false statements.
2. This IIf statement runs a little faster than the normal If statement because it has all the logic/code within a single line.
3. It is an excellent alternative to the normal if statement as long as only 1 line of code will be used in each of the true and false blocks.
Code Editor
I have tried my vb.net code in an online code editor window. You may try the same thing to learn VB.NET in case you cannot install an integrated development environment in your system.
More Examples with the IIF Statement:
Example 1
In this example, we will fix a temperature to a variable and check if it is less than or equal to the normal temperature. If it is above the normal temperature, it means that the patient is suffering from a fever. The appropriate message will be displayed.
Public Module Program Public Sub Main(args() As string) Dim temperature as integer temperature=38 Console.WriteLine(IIf (temperature<;=37,"You are normal","You are suffering from fever. Visit a doctor")) End Sub End Module
Try running the same program with different values of temperature.
Example 2
Here is a program that validates whether the user is a senior citizen or if he must wait to become one. If yes, how long does he have to wait? Using an installed code editor, use message boxes and input boxes in your code. They do not work with online editors.
Public Module Program Public Sub Main(args() As string) Dim age as integer age=57 Console.WriteLine(IIf (age>=60,"You are a senior citizen","You have to wait for " & 60-age & " years to become a senior citizen")) End Sub End Module
Generally, people 60 years and above are considered “senior citizens.” Here, we are considering the same logic.
So, the output for input age “57” is “You have to wait for 3 years to become a senior citizen.”
Conclusion
To put it in simpler terms, the ”Inline If” statement in VBA and VB.NET is useful to quickly type in the code and save time on compilation/execution. If you need to use a single line of code in your If-else blocks, you may use this magical statement/function.