Careful with IIF in VB.NET
In VB.NET, We generally use ternary operator, IIF. Today we came across with some atypical behaviors. Dim a As Boolean = True Dim b As Integer = 1 Dim c As Integer = Nothing Dim d As Integer = CInt(IIf(a = True, b = 3, c.ToString())) What is the result? It throws error. Null cannot be converted to string. But if we use same statements with IF..ELSE, it will give you correct result. If (a = True) Then b = 3 Else Response.Write(c.ToString()) End If It works fine. First observation: In IIF, it evaluates both true and false conditions. That’s why it gives error. Another example: Dim a As Boolean = True Dim b As Integer = 1 Dim c As Integer = 2 Dim d As Integer = CInt(IIf(a = True, b = 3, c=4)) What is the value of B now? It should be 3, right? But, it will be 1. Yes One Second observation: a= true is right, so it goes to b=3 statement. But it won’t consider it as assignme...