Is there a conditional ternary operator in VB.NET?

vb.netOperatorsConditional OperatorShort Circuiting

vb.net Problem Overview


In Perl (and other languages) a conditional ternary operator can be expressed like this:

my $foo = $bar == $buz ? $cat : $dog;

Is there a similar operator in VB.NET?

vb.net Solutions


Solution 1 - vb.net

Depends upon the version. The If operator in VB.NET 2008 is a ternary operator (as well as a null coalescence operator). This was just introduced, prior to 2008 this was not available. Here's some more info: Visual Basic If announcement

Example:

Dim foo as String = If(bar = buz, cat, dog)

[EDIT]

Prior to 2008 it was IIf, which worked almost identically to the If operator described Above.

Example:

Dim foo as String = IIf(bar = buz, cat, dog)

Solution 2 - vb.net

iif has always been available in VB, even in VB6.

Dim foo as String = iif(bar = buz, cat, dog)

It is not a true operator, as such, but a function in the Microsoft.VisualBasic namespace.

Solution 3 - vb.net

If() is the closest equivalent, but beware of implicit conversions going on if you have set Option Strict off.

For example, if you're not careful you may be tempted to try something like:

Dim foo As Integer? = If(someTrueExpression, Nothing, 2)

Will give foo a value of 0!

I think the ? operator equivalent in C# would instead fail compilation.

Solution 4 - vb.net

Just for the record, here is the difference between If and IIf:

IIf(condition, true-part, false-part):

  • This is the old VB6/VBA Function
  • The function always returns an Object type, so if you want to use the methods or properties of the chosen object, you have to re-cast it with DirectCast or CType or the Convert.* Functions to its original type
  • Because of this, if true-part and false-part are of different types there is no matter, the result is just an object anyway

If(condition, true-part, false-part):

  • This is the new VB.NET Function
  • The result type is the type of the chosen part, true-part or false-part
  • This doesn't work, if Strict Mode is switched on and the two parts are of different types. In Strict Mode they have to be of the same type, otherwise you will get an Exception
  • If you really need to have two parts of different types, switch off Strict Mode (or use IIf)
  • I didn't try so far if Strict Mode allows objects of different type but inherited from the same base or implementing the same Interface. The Microsoft documentation isn't quite helpful about this issue. Maybe somebody here knows it.

Solution 5 - vb.net

If(<expression>, <expressionIfNothing>)

If <expression> evaluates to a reference or Nullable value that is not Nothing, the function returns that value. Otherwise, it calculates and returns <expressionIfNothing> (Intellisense)


This is useful for checking that a particular value exists, and if not replacing it.

Example:

If(cat, dog)

Here, if the cat is not null, it will return cat. If it is null, it will return dog. Most of the time you will be using a ternary operator for this scenario. However, if you do not want to return the value you are testing you will have to use this instead:

If(condition, cat(true), dog(false))

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJim CountsView Question on Stackoverflow
Solution 1 - vb.netBeep beepView Answer on Stackoverflow
Solution 2 - vb.netKris EricksonView Answer on Stackoverflow
Solution 3 - vb.netunnknownView Answer on Stackoverflow
Solution 4 - vb.netAranxoView Answer on Stackoverflow
Solution 5 - vb.netEli FryView Answer on Stackoverflow