|
-
Apr 24th, 2010, 02:53 PM
#1
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
-
Apr 24th, 2010, 02:59 PM
#2
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.
-
Apr 24th, 2010, 03:05 PM
#3
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
Last edited by Spoo; Apr 24th, 2010 at 03:10 PM.
Reason: add caveat
-
Apr 24th, 2010, 06:54 PM
#4
Frenzied Member
-
Apr 25th, 2010, 08:38 AM
#5
Re: IIf logic
Zach
More better 
Thanks
Spoo
-
Apr 25th, 2010, 10:58 AM
#6
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
-
Apr 25th, 2010, 11:26 AM
#7
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|