Results 1 to 7 of 7

Thread: IIf logic

  1. #1

    Thread Starter
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    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

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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.

  3. #3

    Thread Starter
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    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

  4. #4
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: IIf logic

    Check Merri's FAQ thread here: http://www.vbforums.com/showthread.php?t=416403

  5. #5

    Thread Starter
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: IIf logic

    Zach

    More better
    Thanks

    Spoo

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    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
  •  



Click Here to Expand Forum to Full Width