IIf Issues

Sean Ronan (from Activepixels.co.uk) was asking me the other day why he was receiving a “Cannot convert ‘’ to DateTime” when writing:

IIf(String.IsNullOrEmpty(DateOfBirth.Text), Nothing, CDate(DateOfBirth.Text))

Thanks to Doug Setzer from 27Seconds in pointing him in the right direction, unlike C#’s ability to write:

(TrueFalseStatement ? TruePart : FalsePart)

VB’s IIf is a function not a statement so unlike C#’s equivielent which is run un-evaluated, VB’s IIf function evaluates both sides of the statement regardless of whether the statement is true of false.

Yet another reason to use C# IMNSHO :-)

Subscribe to TSD

Don’t miss out on the latest posts. Sign up now to get access to the library of members-only posts.
jamie@example.com
Subscribe