The Difference Between IIf and If in VB 9.0

IIf is a function that exists in older versions of Visual Basic (before .Net) and in Visual Basic for Applications (VBA). The function has the following syntax:

    IIf( <expression>, <truepart>, <falsepart> )

It will return either the true part or the false part, depending on the evaluation of the expression in the first argument.

The IIf function has been carried forward into the Visual Basic.Net language, through the Microsoft.VisualBasic.dll assembly. Recently, I discovered that this function has some potential downsides to its use. For example, if you try:

    IIf ( 1 = 1, MsgBox("True"), MsgBox("False") )

then you'll see the message "True" followed immediately by the message "False". The reason for this is that because IIf is a function, all the arguments to the function are evaluated regardless of which one is eventually returned. Effectively, the compiler is doing the following:

expr1 = ( 1 = 1)

expr2 = MsgBox("True")

expr3 = MsgBox("False")

IIf( expr1, expr2, expr3 )

The only way to avoid such side effects is to rewrite the code as follows:

    If( 1 = 1 ) Then

        MsgBox("True")

    Else

        MsgBox("False")

    End If

In VB 9.0 (the version of VB that ships with Visual Studio 2008), there is now a "ternary" version of If that behaves as you would expect, so:

    If( 1 = 1, MessageBox.Show("True"), MessageBox.Show("False") )

will just display the message "True". In addition, if you only supply two arguments, then the If function can also act as a null-coalescing operator:

    Dim name As String = Nothing

    If ( name, "Default name" )

returns "Default name", which is especially useful if you're working with Nullable types and need to convert them into some 'default value' to display on a form or web page.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Vroom, vroom!

In a recent post I mentioned that I’d be off with some friends for a boys’ weekend away. And so it was that on the weekend of 8th-10th May my friends and I found ourselves in the picturesque village of Hook Norton, Oxfordshire (famous for the brewery of the same name).

As well as the usual eating, drinking and Xbox-playing, we went on the most amazing driving experience, where we had the opportunity to drive performance cars at high speed around a disused airfield.

Picture of fast cars

I confess that prior to the weekend, I was sceptical about whether or not I’d enjoy the event as I’m not that ‘into’ cars…but after having had the drive of my life, I’m now a total petrol-head!

Each of us had a turn in four different cars (accompanied by an instructor), followed by a “hot lap” where the instructor takes you round the course at flat-out speed. The table below shows the scores our instructors gave us, together with their comments:

Car Driver Rating Comments
Porsche 911 Andrew 83% OK.
  James 83% OK.
  Jason 80% OK.
  Nick 80% OK.
Ferrari 360 Andrew 84% Good drive.
  James 83% Well done.
  Jason 82% Well done.
  Nick 83% Well done, keep speed under control.
Subaru Impreza Andrew 79% Good steady run, well done.
  James 85% Became a lot smoother, well done.
  Jason 83% Fast, smooth drive.
  Nick 85% Quick drive, well done.
Lotus Elise Andrew 86% Good progress, work on braking.
  James 91% Very good commitment.
  Jason 90% Good speed & awareness.
  Nick 89% Fast, committed drive.

A quick tot-up of the individual ratings produces the following averages:

Driver

Average Rating

Andrew

83.00%

James

85.50%

Jason

83.75%

Nick

84.25%

I was very surprised to come out with the highest average, since I’m the oldest amongst us (and therefore, a bit more cautious) and I’ve also had a spate of minor incidents in my own car recently.

Nevertheless, it’s nice to be able to prove now and again that us oldies have still got it. Eat my dust!

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5