Strongly-Typed DataSet with Is<ColumName>Null and Iif function

Here's the story... I'm using VB.Net 2005 with a strongly-typed dataset.  Both Option Explicit and Option Strict are enabled.  The underlying database is SQL 2005 Express (but that's probably not relevant).

In one of my Tables, a string column allows for nulls, so it automatically generated a Is<ColumnName>Null function for me.   So far, so good...

However, when I use the Is<ColumnName>Null inside an IIF() Statement, both the true and false parts get evaluated (even if the expression evaluates to True)

Take a look at the following to see what I'm talking about...

    Overrides Function ToString() As String
        Dim sb As New System.Text.StringBuilder
        Dim dr As SOSOSDataSet.AccountsRow

        For Each dr In _ds.Accounts                             '<- ds = Strongly-Typed DataSet with Account table
            sb.Append("Account=" & dr.Account & vbCr)

            ' Neither of the 2 following lines work!  The dr.Account_FullName method gets evaluated regardless
            ' of the outcome of the expression.  And, when it evaluated to null, it generates an exception
            'sb.Append(vbTab & "FullName=" & CStr(IIf(dr.IsAccount_FullNameNull, "", dr.Account_FullName)) & vbCr)
            'sb.Append(vbTab & "FullName=" & IIf(dr.IsAccount_FullNameNull, "", dr.Account_FullName).ToString & vbCr)

            ' Oddly enough, using an normal If/Else/End if statement works!
            If dr.IsAccount_FullNameNull Then                '<- Automatically generated IsNull function
                sb.Append(vbTab & "FullName=" & vbCr)
                sb.Append(vbTab & "FullName=" & dr.Account_FullName & vbCr)
            End If

            ' Using the old-school IsDBNull function and the "native" DataRow interface
            ' Doesn't work any better inside an IIF statement
            'sb.Append(vbTab & "FullName=" & CStr(IIf(IsDBNull(dr("Account_FullName")), "", dr.Account_FullName)) & vbCr)

            ' However, this works just fine!
            If IsDBNull(dr("Account_FullName")) Then
                sb.Append(vbTab & "FullName=" & vbCr)
                sb.Append(vbTab & "FullName=" & dr.Account_FullName & vbCr)
            End If

            'sb.Append(vbTab & "Description=" & CStr(IIf(dr.IsAccount_FullNameNull, "", dr.Account_FullName))) & vbCr)
            sb.Append(vbTab & "Disabled=" & dr.Account_Disabled.ToString & vbCr)
            sb.Append(vbTab & "LockedOut=" & dr.Account_LockedOut.ToString & vbCr)
            sb.Append(vbTab & "PwdRequired=" & dr.Account_PwdRequired.ToString & vbCr)
            sb.Append(vbTab & "LockedOut=" & dr.Account_PwdChangeable.ToString & vbCr)
            sb.Append(vbTab & "PwdRequired=" & dr.Account_PwdExpires.ToString & vbCr)

        Return sb.ToString
    End Function

I think I'm going crazy... either that or I've found a bug.

Isn't the IIF function supposed to "Short Cut" the evaluation of the arguments?
LVL 41
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

grayeAuthor Commented:
Hummm... The MSIL shows that it's evaluating both the true and false parameters, poping them on the stack, and then executing the IIF

      L_0047: ldstr "\tFullName="
      L_004c: ldloc.0
      L_004d: callvirt instance bool SOSOS.SOSOSDataSet/AccountsRow::IsAccount_FullNameNull()
      L_0052: ldstr ""
      L_0057: ldloc.0
      L_0058: callvirt instance string SOSOS.SOSOSDataSet/AccountsRow::get_Account_FullName()
      L_005d: call object [Microsoft.VisualBasic]Microsoft.VisualBasic.Interaction::IIf(bool, object, object)

So, I guess it's just me...

Is there an "in-line" equivalent of IIF that mirrors the behavior of the "? :" operators in C#
When doing the iif, first it evaluates all arguments, then if performs the condition. So, when the expression is null, it will first try to evaluate it regardless of the condition - and that's why it throws an exception. The exception is not being thrown by iif but by your dataset, since iif is trying to retrieve the null value - keep that in mind.

I would do something like this
Dim FullName As String = ""
If Not dr.IsAccount_FullNameNull Then FullName = dr.Account_FullName

in your string builder:

sb.append("FullName=" & FullName)



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.