IIF referencing both conditional parts regardless of condition?

S

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
 
S

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...
 
C

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
 
S

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.
 
C

Chris Dunaway

Steven said:
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.
 
H

Herfried K. Wagner [MVP]

Steven Nagy said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top