Quick Tip: IIf isn't doesn't like null values!

Published Dec 14, 2011 (12 years ago)
Danger icon
The last modifications of this post were around 12 years ago, some information may be outdated!

Quick Tip

I've spent a good day beating my head up against a wall, and discovered that our VB ternary friend can be a little unfriendly at times...

For those if you still in Visual Basic land (I delve back and forth these days), you know that there is no default ternary operator built into the language. However there is a helpful function that gives you the same functionality. In my case, I was doing some simple string formatting type stuff:

However, running this code gives you a lovely NullReferenceException. After some more head-banging-wall action and a wonderful nugget of wisdom from StackOverflow, I realized the reason. IIf is a function provided by the VB.Net framework, it is not it's own operator, like the x ? y : z code works in C#. Therefore, if a null value is passed into a function, that isn't expecting it, it's going to yell at you, instead of short circuiting the evaluation, as you'd expect.

The solution? Use the If operator that became available a while back:

Sometimes one little redundant character makes all the difference... Enjoy!