Page 1 of 2 12 LastLast
Results 1 to 15 of 27
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Why Use IIF (general)

    I have just today seen the 'IIF" statement, by virtue of a client's question to me about Access VBA code.

    I tried it out in Word97SR2 and saw what it did.

    I've read the thread about "solely in Access" and "no it's not".


    If you currently make use of the IIF, what prompted you to prefer that over the regular nestable If-Then-Else?


    I'm struggling to find a reason for ever using IIF in preference to the regular nested version.


    I'm particularly interested in examples where one can do with IIF what can't be done (!) using If-Then-Else.

  2. #2
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Why Use IIF (general)

    There would be no reason to use IIF (Immediate If) in normal VBA coding in preference to the IF Then constract, unless it is part of a query string where the usual IF approach would not work. Although it is included in all versions of VBA, it has little use outside of Access.

    Consider the following

    Sub IIFProblem()
    int1 = 0
    int2 = 2
    MsgBox IIf(int1 = 0, "", int2 / int1)
    End Sub

    This will result in a divide by zero error, as all arguments are evaluated, even though in this case the last is irrelevant.

    So handle with care and only if you need to.

    Andrew

  3. #3
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Why Use IIF (general)

    IIF is a shortcut. It can help to avoid temporary variables.

    strSQL = "SELECT * FROM EMPLYOYEES WHERE EMPLOYEE_ID " & IIF(strID = "", "IS NULL"," = " & strID)
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  4. #4
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Why Use IIF (general)

    > This will result in a divide by zero error, as all arguments are evaluated,

    Right. It's the same argument as that for couching some conditions in nested IF statements rather than employing AND technology.

  5. #5
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Why Use IIF (general)

    I don't think that I have ever used the iif. To me it seems to obfuscate the code. But, check out the next to last "tip" at http://msdn.microsoft.com/library/default...._tentipsvba.asp. Click on the "Use the IIf Function for Simple True-False Comparisons." Can Microsoft be wrong? <img src=/S/wink.gif border=0 alt=wink width=15 height=15>
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  6. #6
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Why Use IIF (general)

    In C# (and other C-based languages??) it's called the Ternary Operator and it looks like this:

    expression ? true-part : false-part

    so iif(x=1,"True","False") becomes

    x=1?"true":"false"

    Ain't that sweet? I have had affectations for immediate if and now have the same for ?:

    Maybe a bit cryptic, but it's really a lovely construct if for no other reason
    than it is succinct. Some say elegant.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  7. #7
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Why Use IIF (general)

    > Ain't that sweet?
    <img src=/S/notmyfault.gif border=0 alt=notmyfault width=15 height=15> That is plug-ugly! It could only appeal to a Unix programmer! Might be good job security, though. Maybe we should switch back to RPG. Object-oriented RPG, I love it, let's call it RP++. --Sam <img src=/S/sarcasm.gif border=0 alt=sarcasm width=15 height=15>
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  8. #8
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Why Use IIF (general)

    I knew I'd get your goat on that one...

    Ever heard of APL? Now that's cryptic.

    Ternary operators unite!
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  9. #9
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Why Use IIF (general)

    If I remember (and that's doubtful), APL is the C5-A (that's a monster carge plane) of programming languages. They got the design-specs for both of them identically: they took a survey of all the pilots/programmers in the world and asked them what they wanted to have in an airplane/language. They then took the survey and repackaged it as the design specs!

    I worked on C5's for a time and they had 5 or so navigation systems. Someone (see above) had the "bright(?)" idea to create a system to monitor all of these nav-systems and do an automatic maintenance report on any system that didn't agree with the others. A good idea? NOT! Whenever you got one of those reports, guess what you did? <img src=/S/yep.gif border=0 alt=yep width=15 height=15>You replaced the system that was comparing all of the nav-systems. Nav systems have to be reliable; the write-up system did not. I think that the same holds true in programming: new is not usually better, cryptic is never better.

    With that, I will step-down from my soap-box and probably trip. <IMG SRC=http://www.mtl.com/sam/images/sneeze.gif>
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  10. #10
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Why Use IIF (general)

    Sammy, baby, you need a day off...
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  11. #11
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Why Use IIF (general)

    Can't, I've got a C5 to get flying in Excel. It takes multi-column CSV files, rearranges the columns into separate sheets (col 1 is the independent variable, rest of the columns are sets of dependent variables. Then it plots cross-sections of these sheets at various user-selected rows.
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  12. #12
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Why Use IIF (general)

    > Can Microsoft be wrong?

    Ask not "Can Microsoft be wrong?" but "Could they have picked a poorer example of use?"

  13. #13
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Why Use IIF (general)

    > it's called the Ternary Operator

    Rinse your mouth!

    When I was your age (VBG!) we used "FORTRAN II" and used a 3-way IF statement. I look back at those days in horror, but it was all we had,.....

    <pre>If (JDIM-30)10,20,30
    </pre>


    where the 10, 20 and 30 represented statement numbers to which we should GoTo.

  14. #14
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Why Use IIF (general)

    > APL? Now that's cryptic.

    Hmmm. I have a copy of Kenneth Iverson's book in front of me. Chapter 2, in which he describes each part of the IBM 7090 in APL is a masterpiece. The complete instruction fetch IN MICROCODE is expressed in only 18 lines of APL. The bubble-sort is but 10 lines.


    "Elegant, Concise" are words that come to mind.

  15. #15
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Why Use IIF (general)

    > Ask not "Can Microsoft be wrong?" but "Could they have picked a poorer example of use?"
    I could not have put it better myself! It was worse than their classic poor examples!

    > we used "FORTRAN II
    Ah, the good old days. When I retire, I'm going to write an object-oriented FORTRAN++ compiler and integrate it with OpenOffice Excel. <img src=/S/evilgrin.gif border=0 alt=evilgrin width=15 height=15>
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •