1. Computing

Two new ways to use If in Visual Basic .NET

... and a refresher on IIF too.


Updated October 14, 2011

At first I thought, "Well ... this is dumb!"

VB.NET now features a new flavor of the If operator. But before we get to that, let's review.

The C programmers have always had a "ternary" conditional operator identified by "?". So VB.NET implemented basically the same thing. Ours is the IIf function. It works like this:

Dim AboutVB As String = "Great"
MsgBox( _
   IIf(AboutVB = "Great", "Oh Ja!", "Bummer"))

Which displays a MsgBox containing, "Oh Ja!".

VB.NET 2008 has now added two more flavors of the same thing but they have recycled the old standby If (just one "I") to identify them. So the only difference between this If and the old one is context. When you use If this way ...

If(AboutVB = "Great", "Oh Ja!", "Bummer"))

... this new If does the same thing as IIF. But when the second and third arguments actually do something, it can lead to problems. For example,

Dim ShouldHardDiskBeDeleted As String = "NO"
MsgBox( _
   IIf( _
   ShouldHardDiskBeDeleted = "NO", _
   JustReturnAndDoNothing(), _

Function WipeTheHardDisk()
   Console.WriteLine("Hard Disk Deleted")
   Return "Hard Disk Deleted"
End Function
Function JustReturnAndDoNothing()
   Console.WriteLine("Nothing Done")
   Return "Nothing Done"
End Function

Since the condition in the first argument returns True, the MsgBox reports that nothing was done. But, as a cautious programmer, I put those logging statements into the functions to make sure. And I also get this in the Output window:

Nothing Done
Hard Disk Deleted

Even though my variable said, "Don't delete the hard disk!" the code went ahead and did it anyway VB.NET's IIF evaluate both objects in the argument list anyway.

This can be a problem ... so VB.NET 2008 allows you to use the same statement with just one "I" to solve it.

If( _
ShouldHardDiskBeDeleted = "NO", _
JustReturnAndDoNothing(), _

Now the Output window reports just:

Nothing Done

We're finally to the point where I thought, "This is dumb!"

The If operator also allows you to use it with a two argument signature. Here's Microsoft's official doc on it:

"The first argument must be a reference or nullable type. If the first argument evaluates to Nothing, the value of the second argument is returned. In all other cases, the value of the first argument is returned."

In more traditional code, this is equivalent to:

Dim FirstArg As Object
Dim SecondArg As Object = "This is an object"

If FirstArg = Nothing Then
   Result = SecondArg
   Result = FirstArg
End If

Big Whoop!

But then I realized that this actually solves a problem that often plagues programmers when they're dealing with objects that might or might not be initialized. You can use this statement to ensure that your code doesn't crash by always returning a valid object.

Consider this code:

Dim NothingObject As Object
Dim SomethingObject As Object = "This is an object"
Dim Result As Object

Result = If(NothingObject, SomethingObject)
Console.WriteLine( _
   "NothingObject is first: " & Result.ToString)
Result = If(SomethingObject, NothingObject)
Console.WriteLine( _
   "SomethingObject is first: " & Result.ToString)

Regardless of how it's coded, the value of the object that is initialized is always returned. So, for example, you could code ...

Result = If(ObjectMightBeNothing, DefaultObject)

... and get a valid object every time.

  1. About.com
  2. Computing
  3. Visual Basic
  4. Using VB.NET
  5. The If Operator In Visual Basic .NET

©2014 About.com. All rights reserved.