VbNet · Console
Imports System
Imports Microsoft.VisualBasic
Public Module Module1
Public Sub Main()
Dim didItWork as Boolean = False
Dim myTestObject as Test = Nothing
' works, due to IF(). Only the 'true' value is calculated
didItWork = If(myTestObject Is Nothing, False, myTestObject.MyValue)
Console.WriteLine("Did If() work?: " & didItWork.ToString())
' does not work, due to IIF(). Both True and False conditions are calculated regardless of the original test condition.
' it fails because myTestObject is null, so trying to access one of its properties causes an exception.
Try
didItWork = IIF(myTestObject Is Nothing, False, myTestObject.MyValue)
Console.WriteLine("Did IIF() work?: " & didItWork.ToString())
Catch ex as Exception
Console.WriteLIne("Error was thrown from IIF")
End Try
End Sub
End Module
Public Class Test
Public Property MyValue as Boolean = True
End class