One of my all time favorite features in C# and CPP has been the conditional operator (?:). The brevity and elegance introduced by this operator in the code is unparalleled. We had IIF in all the previous versions of VB.net, which was not an operator in the true sense (It was a call to a function). But now with Visual Studio 2008, we have taken an "i" from this function and promoted it to operator status. "IF" can now be used instead of "IIF". It provides full functionality of the IIF function with some very interesting additions of its own, including short-circuiting. It even comes in two flavors: the ternary flavor which mimics the functionality of the IIF function while adding some more to it, and the binary flavor which introduces a new sort of comparison/assignment. Before I start getting too vague, I will just delve into the details of how to use the greatness called the IF operator!
Note: I will touch only the ternary flavor in this blog, and will follow up with the binary if operator and other interesting examples for if in blogs to come.
The first and most basic use of IF is the universal "compare, choose and assign" which is the functionality of conditional operators, and the iif function. The following are the definitions of the three operands of IF(TestExpression, TruePart, FalsePart):
Dim return = If(var Is Nothing, foo1(), var.foo())
Now lets look at some code that shows the ternary flavor of the IF operator
Module Module1 Public Class Number Private _RealPart As Double Public Property RealPart() As Double Get Return _RealPart End Get Set(ByVal value As Double) _RealPart = value End Set End Property Private _ComplexPart As Double Public Property ComplexPart() As Double Get Return _ComplexPart End Get Set(ByVal value As Double) _ComplexPart = value End Set End Property Public Sub New(ByVal pRealPart As Double, Optional ByVal pComplexPart As Double = 0.0) _RealPart = pRealPart _ComplexPart = pComplexPart End Sub Public Overrides Function ToString() As String Return If(_ComplexPart = 0.0, _RealPart.ToString(), _ _RealPart.ToString & " + " & _ComplexPart.ToString() & "i") End Function End Class Sub Main() Dim i As New Number(1) 'The If operator looks checks the number to see if it is real or complex, and prints accordingly Dim NumberPrinter = Function(arg As Number) arg.RealPart.ToString & " + " & arg.ComplexPart.ToString() & "i" Console.WriteLine("i is a real number:") Console.WriteLine(If(i.ComplexPart = 0.0, i.RealPart.ToString(), NumberPrinter(i))) 'Or the If operator can be moved inside the lambda to add to the elegance Dim j As New Number(1, 2) Dim NumberPrinter2 = Function(arg As Number) If(arg.ComplexPart = 0.0, arg.RealPart.ToString(), _ arg.RealPart.ToString & " + " & arg.ComplexPart.ToString() & "i") Console.WriteLine("j is a complex number:") Console.WriteLine(NumberPrinter2(j)) 'You can also use the short-circuiting feature of the if operator Dim k As Number Console.WriteLine("k does not have any value, so we print j again:") Console.WriteLine(If(k Is Nothing, j.ToString, k.ToString)) k = New Number(3, 4) Console.WriteLine("And now for k with a value:") Console.WriteLine(If(k Is Nothing, j.ToString, k.ToString)) End Sub End Module
Notice how short-circuiting is used in the above code when k does not have a value (the first call). The call to a method from a null object is never evaluated.
For those addicted IIF users wondering if they can still use the legacy IIF function in Orcas, the answer is yes. We still support IIF, but with all the amazing things that you can do with the IF operator, why would you want to keep on typing those extra "i"s? And while you explore the many, many things that you can do with IF, I will be writing and posting more IF code and then some more! Until then,
Dim blog_next = If(bIFoundMoreInterestingScenarios, blogDelvingIntoIF, blogUnknown)
If you know what I mean :)