Ternary operator in VB.NET

We all know about Ternary operator in C#.NET. I am a big fan of ternary operator and I like to use it instead of using IF..Else. Those who don’t know about ternary operator please go through below link.

http://msdn.microsoft.com/en-us/library/ty67wk28(v=vs.80).aspx

Here you can see ternary operator returns one of the two values based on the condition. See following example.

bool value = false;
string output=string.Empty;

//using If condition
if (value==true)
output ="True";
else
output="False";

//using tenary operator
output = value == true ? "True" : "False";

In the above example you can see how we produce same output with the ternary operator without using If..Else statement.

Recently in one of the project I was working with VB.NET language and I was eager to know if there is a ternary operator equivalent there or not. After searching on internet I have found two ways to do it. IF operator which works for VB.NET 2008 and higher version and IIF operator which is there since VB 6.0.

So let’s check same above example with both of this operators. So let’s create a console application which has following code.

Module Module1

Sub Main()
Dim value As Boolean = False
Dim output As String = String.Empty

''Output using if else statement
If value = True Then
output = "True"
Else
output = "False"

Console.WriteLine("Output Using If Loop")
Console.WriteLine(output)

output = If(value = True, "True", "False")

Console.WriteLine("Output using If operator")
Console.WriteLine(output)

output = IIf(value = True, "True", "False")

Console.WriteLine("Output using IIF Operator")
Console.WriteLine(output)

Console.ReadKey()

End If
End Sub

End Module

As you can see in the above code I have written all three-way to condition check using If.Else statement and If operator and IIf operator. You can see that both IIF and If operator has three parameter first parameter is the condition which you need to check and then another parameter is true part of you need to put thing which you need as output when condition is ‘true’. Same way third parameter is for the false part where you need to put things which you need as output when condition as ‘false’.

Now let’s run that application and following is the output as expected.

Ternary Operator in Vb.NET

That’s it. You can see all three ways are producing same output. Hope you like it. Stay tuned for more..Till then Happy Programming.

Shout it
Published Wednesday, April 4, 2012 3:03 AM by Jalpesh P. Vadgama
Filed under: , , ,

Comments

# re: Ternary operator in VB.NET

Tuesday, April 3, 2012 7:42 PM by Tyrone

Be careful with using IIF and VB. Microsoft recommends using If(condition,True,False)

# re: Ternary operator in VB.NET

Wednesday, April 4, 2012 4:24 AM by PB

IIf(Expression, TruePart, FalsePart) evaluates both the TruePart and FalsePart input parameters regardless of the expression entered. This can cause problems if the TruePart/FalsePart parameters include method calls which affect state.

If(Expression, TruePart, FalsePart) is the way to go if you want a more 'traditional' one line if/else...

# re: Ternary operator in VB.NET

Wednesday, April 4, 2012 2:09 PM by RichardD

As Tyrone said, avoid IIF. Using IIF, both operands will be evaluated before the condition; with IF, only the relevant operand will be evaluated.

For example:

Dim o As Object = Nothing

Dim s As String = IIF(o Is Nothing, "-", o.ToString())

This throws a NullReferenceException evaluating "o.ToString()", even though the condition is True.

Dim o As Object = Nothing

Dim s As String = IF(o Is Nothing, "-", o.ToString())

This works as expected.

# re: Ternary operator in VB.NET

Wednesday, April 4, 2012 4:06 PM by James

The C#/VB examples given aren't equivalent. Use If() (as opposed to IIf) if you want to use an equivalent operation. Furthermore 'If(value, "True", "False")' is more succinct. Also if the aim is to convert a bool to a string representation you can just call bool.ToString().

# re: Ternary operator in VB.NET

Thursday, April 5, 2012 2:23 AM by Jalpesh P. Vadgama

@Trone -  I Agree