VB.Net Tip: IIF is a function, not a language feature
I think i've found a nice way to demonstrate this age old gotcha in VB.
What result do you expect from this line of VB code?
Do you expect only the first message box to show up?
You might think it would just execute the:
But you'd be as wrong as a fez with a brim.
BOTH message boxes will come up, one after the other. Try it and see.
Is this a bug? Is this insane? Is this dangerous?
No, no and yes occasionally. The thing is that
IIF is just a built in function. And when you try to send expressions to it, they get evaluated before the function sees them. The same happens any time you send expressions to any function.
So it's not a bug, and it's also unlikely to change in any version of VB, or any future version of the IIF function. So it's worth taking the time to remember it, understand it and work with it.
Now when is this likely to pose a problem?
To avoid a divide-by-zero error, many a coder has tried this:
And they will of course still get a "Divide By Zero" error, if the divisor equals zero.
Again, that's because IIF is just a function. And like all functions in vb, it doesn't accept expressions as parameters. So the expressions must be evaluated before they are passed to the IIF function. Hence the error.
The longer syntax, using full if statements, will work as desired:
And that's pretty much the best way to skin this little kitten.
(thanks to derek for reminding me to post about this)
One little footnote, is that in Excel, if you type:
...you won't get an error message -- because some sort of shortcutting is happening, and the false code path isn't executed.
So, to get a "
#DIV/0!" error message, you'd need to get that particular trouser leg of the if statement to fire, like so:
I'm currently writing a book about how to build your first product. If building a product is something you'd like to do, then sign up to be notified when the book is available.