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