Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 294667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:23:16+00:00 2026-05-12T06:23:16+00:00

I recently learned of the IIF(A,B,C) function. I’m a long time VB/VB.NET Coder who

  • 0

I recently learned of the IIF(A,B,C) function. I’m a long time VB/VB.NET Coder who recently spent a lot of time coming up to speed in SQL coding.

One (obvious) common thing to do in SQL is something like the following:

select (case where @var = 0 then MyTable.Val1 else MyTable.Val2 end) from MyTable

IIF(A,B,C) will allow me to do this in VB.NET… all on one line.

However, I have read that both B and C are evaluated no matter what A evaluates to.

I can think of some obvious situations where this is a bad thing such as:

Dim X as integer = IIF(SomeBoolean = true, ExpensiveFunction1(), ExpensiveFunction2())

As I will be including this in my repertoire, are there any more subtle situations where I may get myself into trouble using IIF?

It’s a pretty big departure in some situations from using the old fashioned:

Dim X as integer
if SomeBoolean = true then
  X = ExpensiveFunction1()
else
  X = ExpensiveFunction2()
end if

I’m hoping to save myself some annoying performance issues and/or bugs in the future.

Update 2016

For the past few years, a new VB.NET feature exists which eliminates the need to use the IIF() function.

if(Something = true, ExecuteA(), ExecuteB())

Only ExecuteA() OR ExecuteB() are executed. Finally, inline IF with shortcircuiting.

So, if you are using later versions of VB.NET (as of 2016), use this instead if you can.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T06:23:16+00:00Added an answer on May 12, 2026 at 6:23 am

    Here is the most common gotcha.

    Z = iif(y=0, 0, x/y)  'Throws a divide by zero exception when y is 0
    

    Don’t use it to avoid division by zero errors.

    Another possible logic bug is when one side of the iif or the other calls a method that modifies the system state or has output parameters.

    Z = iif(FunctionA(InputOutputParam), FunctionB(InputOutputParam))
    'InputOutputParam is indeterminate or at least ambiguous here.
    

    There really is no good reason to use IIF in my experience. Mostly it is just used to abbreviate code and given the problems it can cause, it just isn’t worth it. Plus, I think it makes the code harder to read.

    The other thing that bites is that it returns a boxed value (ie an Object data type) which you have to cast back to the desired type.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 218k
  • Answers 218k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I made a quick test, and it looks like the… May 12, 2026 at 11:29 pm
  • Editorial Team
    Editorial Team added an answer This is actually a firmware issue. New firmware should fix… May 12, 2026 at 11:29 pm
  • Editorial Team
    Editorial Team added an answer Assuming your source table looks something like this: dfm <-… May 12, 2026 at 11:29 pm

Related Questions

I recently learned of the IIF(A,B,C) function. I'm a long time VB/VB.NET Coder who
I recently learned about the basic structure of the .docx file (it's a specially
Having recently learned of the DebuggerDisplay attribute, I've found it quite useful. However, one
UPDATE: I recently learned from this question that in the entire discussion below, I
I recently had my mind expanded by a new concept: Web Services for Remote

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.