-
IIf logic
I think I know the answer, but just to be sure..
.. why does this IIf statement crash?
Code:
aa = 0
bb = 0
cc = IIf(bb = 0, "n/a", aa / bb)
At face value, since bb = 0, it should set
cc to "n/a", right?
Instead, it crashes, "Run-time error '6' -- Overflow"
(ie, trying to divide by 0)
Spoo
-
Re: IIf logic
One major pitfall with IIf is that every expression will be evaluated first; so if you have divison that results in "division by zero" you really need to first evaluate each variable before attemting to apply any function or operator.
Use ordinary If-Else-End If instead.
-
Re: IIf logic
Rhino
That was my hunch (really - edit: albeit not as well explained as yours),
but nice to hear a PowerPoster confirm it.
Thanks.
Spoo
-
Re: IIf logic
-
Re: IIf logic
Zach
More better :cool:
Thanks
Spoo
-
Re: IIf logic
That just speeds up the function... but doesn't explain why IIF works the way it does, not does it solve the problem.
IIF is a function. As a result, it has to evaluate all of the parameters being passed to it, which is where the problem comes in.
Fortunately thais has been fixed in .NET with the new IF operator, but I realize that is neither here nor now in regards to this thread.
-tg
-
Re: IIf logic
TG
Yes, even more better !
Now that you mention it (and thus force me to hunt it down),
and while it is OT, here is a MSDN link discussing VB6 IIf vs .Net If,
Spoo