Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
IIF() bug or feature?
Message
From
09/02/2011 13:12:23
 
 
To
09/02/2011 11:38:10
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
01499384
Message ID:
01499433
Views:
54
This message has been marked as a message which has helped to the initial question of the thread.
>Hi,
>I have DevideByZero exception in expression when LN_QTY=0D
>
>LN_RESULT = IIf(LN_QTY > 0D, LN_SUM / LN_QTY, -1D)
>
>I expect function return me -1D, but so far as I see, VB compiler evaluate every expression not depends from logical rule?
>
>Is it bug or feature? Is the same in C# ?

Naomi and Bill have pointed out that the LN_SUM/LN_QTY expression will be evaluated and you should use IF() but what are the types of the variables? If they are all doubles then you should not be getting an exception but a Nan result. For example:
Dim LN_QTY As Integer = 0D
Dim LN_SUM As Integer = 100D
'Result is NAN (Infinity in VB.NET):
Dim LN_RESULT As Double = LN_SUM / LN_QTY
'Exception:
Dim LN_RESULT2 As Integer = LN_SUM / LN_QTY
Also, FWIW, the C# equivalent works as expected regardless:
LN_RESULT = LN_QTY > 0d ? LN_SUM/LN_QTY : -1d;
Previous
Reply
Map
View

Click here to load this message in the networking platform