BASIC & Visual Basic DISCUSSION

Is there a ternary operator in VB.NET, and why does IIf evaluate both of its branches?

Started by sarmadrasheed VB.NET If operatorIIf functionternary operatornull coalescingOption Strict
5 replies 248 views 6 participants
Latest activity · 30 Sep 2026

Is there a ternary operator in VB.NET, and why does IIf evaluate both of its branches?

sarmadrasheed BASIC & Visual Basic Forum
#1

I used IIf as a replacement for the C-style ?: operator: rate = IIf(count = 0, 0, total \ count). With count equal to zero this throws a DivideByZeroException, although the condition should have selected the first value. I have also seen code that uses If(a, b, c) written like a function call.

What is the difference between IIf and If, and which one is the real conditional operator?

Community replies 5

Re: Is there a ternary operator in VB.NET, and why does IIf evaluate both of its branches?

#2

IIf is not an operator. It is an ordinary library function in the Microsoft.VisualBasic namespace, and as with any function call, all three arguments are evaluated before it runs. So total \ count is computed even when count is 0, and integer division by zero throws.

It also takes and returns Object, so value types are boxed and, with Option Strict On, the result has to be converted back with CInt or similar.

Re: Is there a ternary operator in VB.NET, and why does IIf evaluate both of its branches?

#3

The real conditional operator, available since Visual Basic 2008, is If with three operands: rate = If(count = 0, 0, total \ count). It looks like a call but is built into the language: only the selected branch is evaluated, and the result type is worked out from the two branches at compile time, so there is no boxing and no cast. Swapping IIf for If is the whole fix for your line.

A detail about your example: the exception appears because \ is integer division. With / the operands are converted to Double, and floating-point division by zero yields infinity or NaN instead of throwing, which would have hidden the problem.

Re: Is there a ternary operator in VB.NET, and why does IIf evaluate both of its branches?

#4

If also has a two-operand form that acts as a null-coalescing operator: If(name, "unknown") returns name unless it is Nothing, in which case it returns the second operand, which is only evaluated when needed.

It works for reference types and for nullable value types, so with Dim limit As Integer? the expression If(limit, 100) gives an Integer that is 100 when no limit was set.

Re: Is there a ternary operator in VB.NET, and why does IIf evaluate both of its branches?

#5

One trap with the three-operand form involves Nothing and value types. Dim x As Integer? = If(valid, 5, Nothing) does not produce a null when valid is False; it produces 0. The compiler types the expression from the branches, sees Integer and Nothing, and in VB Nothing converted to Integer is the default value 0.

Make one branch explicitly nullable: If(valid, 5, CType(Nothing, Integer?)) or If(valid, CType(5, Integer?), Nothing).

Re: Is there a ternary operator in VB.NET, and why does IIf evaluate both of its branches?

#6

In VBA and VB6 there is no If operator, only IIf, with the same evaluate-everything behaviour. IIf(n = 0, 0, total / n) raises run-time error 11, division by zero, in VBA, where / does raise it, unlike .NET. Likewise IIf(obj Is Nothing, "", obj.Name) raises error 91 when obj is Nothing.

In those dialects use a normal If ... Then ... Else block whenever either branch can fail or has a side effect, and keep IIf for choosing between two plain values.

TEP COMMUNITY