By Jim Counts


2009-02-23 03:17:30 8 Comments

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?

3 comments

@Beep beep 2009-02-23 03:46:40

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)

@Zooba 2009-02-23 04:18:26

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

@Greg Hewgill 2009-02-23 04:21:59

...with the important difference that Iif(), being a function, always evaluated both the consequent and the alternative, while the new If only evaluates one of them.

@kbvishnu 2012-01-20 10:21:06

what is it means ? If (condition,true-part,false-part). may i rite ?

@basher 2013-09-18 20:40:16

@VeeKayBee Yes. You're right.

@crush 2015-02-10 19:39:03

I'm a huge C guy, but I find this syntax cleaner than the traditional ternary operator.

@Joseph Nields 2015-04-21 14:55:55

Another important distinction: Iif always returns an object of type Object, whereas If(bool, obj, obj) allows for type-checking with option strict on. (Dim var As Integer = Iif(true, 1, 2) won't compile with option strict on because you could just as easily write Dim var As Integer = Iif(true, new Object(), new Object()). You CAN write Dim var As Integer = If(true, 1, 2) with option strict on though, because it'll check the type returned.)

@unnknown 2012-08-16 15:50:32

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

For example, if your 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

@Mark Hurd 2015-02-04 06:15:31

Just for completeness, the better way to write that expression is Dim foo As Integer? = If( someTrueExpression, New Integer?, 2).

@Heinzi 2015-12-10 14:32:24

Note that this also happen with Option Strict On. The reason is that Nothing in VB.NET is equivalent to C#'s default(T) rather than to null.

@AjV Jsy 2016-06-17 13:59:00

And for anyone puzzled by Integer? it means it's nullable - see stackoverflow.com/questions/3628757/make-an-integer-null

@KyleMit 2017-01-17 19:31:13

For anyone getting stuck on implicit conversion for nullable types - see this answer as to why and this answer for a workaround which casts the argument before returning (CType(Nothing, DateTime?).

@Kris Erickson 2009-02-23 04:25:19

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.

@Beep beep 2009-02-23 14:57:23

Iif is only close to a ternary operator though - which means you couldn't use it in every condition that you would an If Then Else (or ternary operator). For example, Value = Iif(1 = 1, 0, 1/0) would blow up, but Value = If(1 = 1, 0, 1/0) would not ...

@Kris Erickson 2009-02-23 15:22:40

VB doesn't support Short Circuit evaluation (except for the AndAlso operator), so VB programmers don't really expect that they can safely evaluate half an operation. But point taken, also iif is a hack function that was put in for backward compatibility otherwise it would be a real operator.

@HardCode 2009-02-23 17:43:43

Nice to categorize all VB programmers ;-) And there is also IsNot and OrElse to shortcut, so VB does indeed support Short Circuit Evaluation.

@LosManos 2012-12-18 16:10:17

Iif is a regular method call and evaluates all parameters. It is not ternary. Se dotnetslackers.com/VB_NET/…

@Kris Erickson 2012-12-18 19:09:36

As I stated, it is NOT a true operator, and vb6 doesn't support short circuit evaluation so it always evaluates all operations on line anyway.

@Aave 2018-10-17 13:50:14

Method IIf is a part of Microsoft.VisualBasic namespace, that is not compatible with other .NET languages. So better practice is use If() mehod.

Related Questions

Sponsored Content

23 Answered Questions

21 Answered Questions

[SOLVED] What is the "-->" operator in C++?

8 Answered Questions

33 Answered Questions

[SOLVED] What is the !! (not not) operator in JavaScript?

  • 2009-04-24 08:13:58
  • Hexagon Theory
  • 402908 View
  • 2449 Score
  • 33 Answer
  • Tags:   javascript operators

10 Answered Questions

4 Answered Questions

[SOLVED] What does the C ??!??! operator do?

  • 2011-10-19 16:56:59
  • Peter Olson
  • 220225 View
  • 1651 Score
  • 4 Answer
  • Tags:   c operators trigraphs

18 Answered Questions

[SOLVED] Reference — What does this symbol mean in PHP?

8 Answered Questions

7 Answered Questions

Sponsored Content