Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
IIF() bug or feature?
Message
From
10/02/2011 07:34:03
 
 
To
10/02/2011 07:03:52
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
01499384
Message ID:
01499570
Views:
47
Denis,

>>what the reason to have IIF() then? In which case I must use IIF() ?

You can use IIF() if you want to evaluate all the arguments. Some arguments may have side effects, like incrementing a variable or a field

The same applies to the AND operator in C#
&&    The conditional-AND operator (&&) performs a logical-AND of its bool operands, but only evaluates its second operand if necessary

&       The & operator evaluates both operators regardless of the first one's value. For example:

http://msdn.microsoft.com/en-us/library/sbf85k1c.aspx


int i = 0;
if (false & ++i == 1)
{
    // i is incremented, but the conditional
    // expression evaluates to false, so
    // this block does not execute.
}
Same applies to || and |

In VB there is
and   (same as & in C#) http://msdn.microsoft.com/en-us/library/sdbcfyzh.aspx

andalso  (same as && in C#)  http://msdn.microsoft.com/en-us/library/cb8x3kfz.aspx

 Note  
In a Boolean comparison, the And operator always evaluates both expressions, which could include making procedure calls. The AndAlso Operator (Visual Basic) performs short-circuiting, which means that if expression1 is False, then expression2 is not evaluated.
_____________
>Hi Naomi,
>Your answer is usefull for me!
>Nice to know exist if() instead iif() :))))
>
>what the reason to have IIF() then? In which case I must use IIF() ?
>
>Thanks
>Denis
>
>>>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# ?
>>>
>>>Thanks
>>>Denis
>>
>>There is another form of IF in VB which is the shortcut. IIF evaluates all expressions.
>>
>>See this blog post
>>http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/VBNET/if-and-iif
Gregory
Previous
Reply
Map
View

Click here to load this message in the networking platform