• RV
    • Missy – My Home
    • JBAM – Newmar Dutch Star
    • Shaneeda
    • Buying a Used RV
    • Gas vs Diesel RVs
    • Ideal Motorhome
    • Choosing the Ideal RV
  • Cycling
  • Jeep
  • About
  • Flying
    • Vans RV-4
    • Daisy – RV-3B
  • Resources
  • Subscribe

JdFinley.com

Fulltime RV Living Adventures

  • Is Solar For You?
  • Gas vs Diesel RVs
  • Buying a Used RV
  • Choosing the Ideal RV
  • Ideal Motorhome

IF or IIF

August 8, 2011

IF use in Microsoft VB and C# .NetThe IIF function in Microsoft VB is a statement that allows you to evaluate an expression and return one of two objects depending on the result of the evaluation.  Neat.

The problem that I had with IIF is that it  always evaluates the true and false parts of the statement no matter the result of the expression.  Let’s assume this bit of code:

        Dim _result As String
        _result = IIf(_object Is Nothing, “”, _object.PropertyValue)

If _object is not nothing/null then this statement works just fine.  However; if _object is nothing/null, the _object.PropertyValue is evaluated and a null reference exception is thrown.  Pooh….

This can get even worse if you are calling a routine in the true/false portions of the statement.

        IIf(_object Is Nothing, DoSomething(), DoSomethingElse())

In this case, BOTH DoSomething and DoSomethingElse routines will be executed. Double Pooh!

IF to the rescue.  IF now has an overload that allows the same basic functionality of IIF but without the above problems. Additionally, it  is close to matching the functionality of the C# conditional ternary operator (?).

        Dim _result As String
        _result = If(_object Is Nothing, “”, _object.PropertyValue)

OR

        IIf(_object Is Nothing, DoSomething(), DoSomethingElse())

Maybe worth mentioning here, the relatively new Nullable data types can be quite useful. The following is an example of using the Nullable HasValue method instead of an “Is Nothing”/”= null” evaluation.

        If(_object.PropertyName.HasValue, _object.PropertyName, “”)

The point to all of the above is to be careful with IIF when null/nothing objects/properties can exist.  Or, be safe and use IF instead.

(Visited 122 times, 1 visits today)

Related posts:

Default ThumbnailXML Serialization Default ThumbnailObject Serialization (binary) Default ThumbnailIsolated Storage Default ThumbnailMSMQ – Receive Message

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Don’t cry because it’s over, smile because it happened.

— Dr. Seuss

Popular Posts

  • Powermatic Model 90 Wood Lathe & VFD
  • Wood Turning on a Lathe
  • Chainsaw Review: Sportsman 20 in. 52cc
  • Cielo Grande Barbed Wire Fence Repair

Email Notification


 

Posts by Subject

3DPrinting Aircraft Airstrip Bible Bus bus boys Camping Cat Computers Cycling Development Electrical electronics Entertainment Environment Factory Five family FAQ Finances flying Food God Health Holiday Home Humor kayak Life Maintenance Nature Photography Review RV-3 RV-4 sailboat sailing self improvement Sewing solar Travel Video weather wood woodworking X-Country

Posts by Month

© Copyright © 2025 JDFinley.com · All Rights Reserved · Privacy Policy

Unauthorized use and/or duplication of this material without express and written permission from this site’s author and/or owner is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to JD Finley and JdFinley.com with appropriate and specific direction to the original content.