locked
IIF() vs. IF() RRS feed

  • Question

  • User1485408165 posted

    I know with the introduction of VS 2008 and what I guess is VB 9 that Microsoft added the new command IF(eval,true,false) which is suppose to fix a known bug with IIF(eval,true,false) where if the val was false VB would still parse the true value causing possible NPE's or other such problems.

    I have noticed that quite a few posters here still use IIF() and I was wondering why?

    Still using Older Visual Studios?

    Just plain Habit?

    The IIF() command is faster and/or less processor intense then the IF() command.

     

     

    Looking foward to what you guys have to say.

     

    Thanks. 

    Friday, April 2, 2010 12:07 PM

Answers

  • User1408790675 posted

    torren, for me its an old habit to break

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 2, 2010 2:30 PM
  • User397347636 posted

    The behavior of the older "IIf" was not a bug - "IIf" is a function - like any function, all arguments are evaluated whether they're used or not.  The newer "If" operator is not a function - the VB compiler provides the short-circuiting logic when it parses this operator.

    There is no reason to use "IIf" anymore since it was initially designed as a (sloppy) work-around for a missing ternary conditional operator in the VB language. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 3, 2010 12:23 PM
  • User-952121411 posted

     

    I have noticed that quite a few posters here still use IIF() and I was wondering why?

     

    Still using Older Visual Studios?

     

    Yes.

    Just plain Habit?

     

    Yes.

    ...and lastly - some may be unaware of the new command.  Not all developer's seek out every new feature or may have run across it yet to use it.  Honestly, I wouldn't spend too much time (as I am sure you are not) wondering why others do or do not use a new feature.  All that matters is what you are going to use.  It is like some people that still use the concatenate (+) for extreamly long strings instead of a StringBuilder because they have never tried it or know about it because they are converted VB6 developers unaware of a lot of the functionality in the .NET Framework.

    The good thing is you are moving forward with the technology! Smile

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 5, 2010 4:25 PM

All replies

  • User-1636183269 posted

    http://www.fmsinc.com/free/NewTips/NET/NETtip33.asp 

    Visual Basic, VBA, and Visual Basic .NET support the IIF function as an alternative to the If...Then...Else statement. Although this may seem like a shortcut, IIF functions differently than If...Then...Else.

    Friday, April 2, 2010 12:50 PM
  • User1485408165 posted

    No not an If Then Else statement I am talking about the IIF and IF operators.

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

     

    Friday, April 2, 2010 2:08 PM
  • User1408790675 posted

    torren, for me its an old habit to break

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 2, 2010 2:30 PM
  • User397347636 posted

    The behavior of the older "IIf" was not a bug - "IIf" is a function - like any function, all arguments are evaluated whether they're used or not.  The newer "If" operator is not a function - the VB compiler provides the short-circuiting logic when it parses this operator.

    There is no reason to use "IIf" anymore since it was initially designed as a (sloppy) work-around for a missing ternary conditional operator in the VB language. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 3, 2010 12:23 PM
  • User-952121411 posted

     

    I have noticed that quite a few posters here still use IIF() and I was wondering why?

     

    Still using Older Visual Studios?

     

    Yes.

    Just plain Habit?

     

    Yes.

    ...and lastly - some may be unaware of the new command.  Not all developer's seek out every new feature or may have run across it yet to use it.  Honestly, I wouldn't spend too much time (as I am sure you are not) wondering why others do or do not use a new feature.  All that matters is what you are going to use.  It is like some people that still use the concatenate (+) for extreamly long strings instead of a StringBuilder because they have never tried it or know about it because they are converted VB6 developers unaware of a lot of the functionality in the .NET Framework.

    The good thing is you are moving forward with the technology! Smile

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 5, 2010 4:25 PM