Home All Groups Group Topic Archive Search About

IIF referencing both conditional parts regardless of condition?

Author
13 Sep 2006 5:02 AM
Steven Nagy
Hey, take this example code:

IIf(Not (SomeRadiolist.SelectedItem Is Nothing),
SomeRadiolist.SelectedItem.Value, DBNull.Value)

So the idea of this statement was to detect that if there was no
selected item then yield DBNull.Value, otherwise return the value of
the selected item.

Problem is that this still throws a Null Reference exception in the
case where there is no selected item.

So I felt inspired to try to find out why by using Reflector (inspired
by John Skeet's recent blog entry) and this is the code it yielded:

public static object IIf(bool Expression, object TruePart, object
FalsePart)
{
      if (Expression)
      {
            return TruePart;
      }
      return FalsePart;
}

No surprises there. But it doesn't tell me whats wrong with my code.
So I seperated out the logic to this:

            If optInterpreterProvided.SelectedItem Is Nothing Then
                dr.Item(COL_InterpreterProvided) = DBNull.Value
            Else
                dr.Item(COL_InterpreterProvided) =
optInterpreterProvided.SelectedItem.Value
            End If

No error.
Whats going on and where's the flaw? Am I such a VB n00b that I don't
even know that you can't pass a nothing into a method parameter? Is
that the issue here?

Thanks,
Steven

Author
13 Sep 2006 5:19 AM
Steven Nagy
Forget it, I'm an idiot.
Someone tell me how to delete this post before too many people see it
please.
I don't want the smart people to laugh at me. My wife already does that
enough...
Author
13 Sep 2006 5:29 AM
Cor Ligthert [MVP]
Steven,

We did already, but I am glad you replied this yourself because I was
starting a reply.

However, be wise don't use the IIF in VB.Net it is only confusing you and
others as you are reading it the next time.

Cor

Show quoteHide quote
"Steven Nagy" <learndot***@hotmail.com> schreef in bericht
news:1158124797.312625.123870@h48g2000cwc.googlegroups.com...
> Forget it, I'm an idiot.
> Someone tell me how to delete this post before too many people see it
> please.
> I don't want the smart people to laugh at me. My wife already does that
> enough...
>
Author
13 Sep 2006 5:55 AM
Steven Nagy
I don't find it confusing to read...

I just think that IIF should be language specific like it is in C#, not
a method in Microsoft.Visualbasic.dll

Its the colouring.
In C#, the ? and : are visual seperaters making it easy to read.
VB lacks this by default and thus becomes harder to read.

Just my opinion you understand.
Author
13 Sep 2006 1:19 PM
Chris Dunaway
Steven Nagy wrote:
> I don't find it confusing to read...
>
> I just think that IIF should be language specific like it is in C#, not
> a method in Microsoft.Visualbasic.dll
>
> Its the colouring.
> In C#, the ? and : are visual seperaters making it easy to read.
> VB lacks this by default and thus becomes harder to read.
>
> Just my opinion you understand.

But they function differently.  IIF evaluates both the true and false
parts regardless of the value of the boolean expression which you
discovered.  It does this because it is a function.  The C# tertiary
operator (?:) does not evaluate both.
Author
13 Sep 2006 1:22 PM
Herfried K. Wagner [MVP]
"Steven Nagy" <learndot***@hotmail.com> schrieb:
>I don't find it confusing to read...

Hm...  I find it confusing if the parts connected by '?' and ':' get too
long.  However, that's just my personal preference.

> I just think that IIF should be language specific like it is in C#, not
> a method in Microsoft.Visualbasic.dll
>
> Its the colouring.
> In C#, the ? and : are visual seperaters making it easy to read.
> VB lacks this by default and thus becomes harder to read.

As 'IIf' is a normal function in VB, it gets colored the way all other calls
are colored.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>