{"id":4623,"date":"2008-03-12T01:36:00","date_gmt":"2008-03-12T01:36:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vbteam\/2008\/03\/12\/orcas-introduces-the-if-operator-a-new-and-improved-iif-sophia-salim\/"},"modified":"2024-07-05T14:32:43","modified_gmt":"2024-07-05T21:32:43","slug":"orcas-introduces-the-if-operator-a-new-and-improved-iif-sophia-salim","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/vbteam\/orcas-introduces-the-if-operator-a-new-and-improved-iif-sophia-salim\/","title":{"rendered":"Orcas introduces the IF operator &#8211; a new and improved IIF (Sophia Salim)"},"content":{"rendered":"<\/p>\n<p>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 <strong>IIF<\/strong> 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 &quot;i&quot; from this function and promoted it to operator status. <strong>&quot;IF&quot; <\/strong>can now be used instead of <strong>&quot;IIF&quot;<\/strong>. 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 <strong>ternary flavor<\/strong> which mimics the functionality of the IIF function while adding some more to it, and the <strong>binary flavor<\/strong> 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 <strong>IF operator<\/strong>!<\/p>\n<p>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.<\/p>\n<p>The first and most basic use of IF is the universal &quot;compare, choose and assign&quot; 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):<\/p>\n<ol>\n<ol>\n<li><strong>TestExpression, <\/strong>is any expression that produces a boolean value or a value convertible to boolean. With option strict on the expression needs to produce a value that is either boolean or has a widening conversion to boolean.<\/li>\n<li><strong>TruePart, <\/strong>is the return value of the IF operator if the test expression above evaluates to true. Note that the return <strong>type<\/strong> of the operator is decided based on the wider of the types of the truepart and the falsepart, e.g, if the truepart is an integer and the falsepart is a decimal, then decimal which is the wider of the two types will be the return value&#8217;s type. For details on type conversions and for a list of wider and narrower types, please <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/k1e94s7e(VS.80).aspx\">refer here<\/a><\/li>\n<li><strong>FalsePart,<\/strong>&#160; is the return value of the IF operator if the test expression evaluates to false. Here in lies the short-circuiting. If the test expression evaluates to true, the FalsePart is just <strong>ignored. <\/strong>The compiler will not touch it. It will not evaluate it. So as long as your test expression is true, you can do null references here, call functions on objects that do not actually exist, or anything that the parser will let you get away with. It will just not be evaluated. (Please do not think that I am recommending you to do any of the afore-mentioned evils, but as long as your test expression remains true you can get away with this). This can be very useful while calling a function on a variable which may not be instantiated, You can simply do:<\/li>\n<pre class=\"code\"><span style=\"color: blue\">Dim return <\/span>= <span style=\"color: blue\">If<\/span>(var <span style=\"color: blue\">Is Nothing<\/span>, foo1(), var.foo())<\/pre>\n<p>    <a href=\"http:\/\/11011.net\/software\/vspaste\"><\/a><\/ol>\n<\/ol>\n<p>Now lets look at some code that shows the ternary flavor of the IF operator<\/p>\n<pre class=\"code\"><span style=\"color: blue\">Module <\/span>Module1\n    <span style=\"color: blue\">Public Class <\/span>Number\n        <span style=\"color: blue\">Private <\/span>_RealPart <span style=\"color: blue\">As Double\n        Public Property <\/span>RealPart() <span style=\"color: blue\">As Double\n            Get\n                Return <\/span>_RealPart\n            <span style=\"color: blue\">End Get\n            Set<\/span>(<span style=\"color: blue\">ByVal <\/span>value <span style=\"color: blue\">As Double<\/span>)\n                _RealPart = value\n            <span style=\"color: blue\">End Set\n        End Property\n        Private <\/span>_ComplexPart <span style=\"color: blue\">As Double\n        Public Property <\/span>ComplexPart() <span style=\"color: blue\">As Double\n            Get\n                Return <\/span>_ComplexPart\n            <span style=\"color: blue\">End Get\n            Set<\/span>(<span style=\"color: blue\">ByVal <\/span>value <span style=\"color: blue\">As Double<\/span>)\n                _ComplexPart = value\n            <span style=\"color: blue\">End Set\n        End Property\n        Public Sub New<\/span>(<span style=\"color: blue\">ByVal <\/span>pRealPart <span style=\"color: blue\">As Double<\/span>, <span style=\"color: blue\">Optional ByVal <\/span>pComplexPart <span style=\"color: blue\">As Double <\/span>= 0.0)\n            _RealPart = pRealPart\n            _ComplexPart = pComplexPart\n        <span style=\"color: blue\">End Sub\n        Public Overrides Function <\/span>ToString() <span style=\"color: blue\">As String\n            Return If<\/span>(_ComplexPart = 0.0, _RealPart.ToString(), _\n                      _RealPart.ToString &amp; <span style=\"color: #a31515\">&quot; + &quot; <\/span>&amp; _ComplexPart.ToString() &amp; <span style=\"color: #a31515\">&quot;i&quot;<\/span>)\n        <span style=\"color: blue\">End Function\n    End Class\n    Sub <\/span>Main()\n        <span style=\"color: blue\">Dim <\/span>i <span style=\"color: blue\">As New <\/span>Number(1)\n        <span style=\"color: green\">'The If operator looks checks the number to see if it is real or complex, and prints accordingly\n        <\/span><span style=\"color: blue\">Dim <\/span>NumberPrinter = <span style=\"color: blue\">Function<\/span>(arg <span style=\"color: blue\">As <\/span>Number) arg.RealPart.ToString &amp; <span style=\"color: #a31515\">&quot; + &quot; <\/span>&amp; arg.ComplexPart.ToString() &amp; <span style=\"color: #a31515\">&quot;i&quot;\n        <\/span>Console.WriteLine(<span style=\"color: #a31515\">&quot;i is a real number:&quot;<\/span>)\n        Console.WriteLine(<span style=\"color: blue\">If<\/span>(i.ComplexPart = 0.0, i.RealPart.ToString(), NumberPrinter(i)))\n        <span style=\"color: green\">'Or the If operator can be moved inside the lambda to add to the elegance\n        <\/span><span style=\"color: blue\">Dim <\/span>j <span style=\"color: blue\">As New <\/span>Number(1, 2)\n        <span style=\"color: blue\">Dim <\/span>NumberPrinter2 = <span style=\"color: blue\">Function<\/span>(arg <span style=\"color: blue\">As <\/span>Number) <span style=\"color: blue\">If<\/span>(arg.ComplexPart = 0.0, arg.RealPart.ToString(), _\n                                                        arg.RealPart.ToString &amp; <span style=\"color: #a31515\">&quot; + &quot; <\/span>&amp; arg.ComplexPart.ToString() &amp; <span style=\"color: #a31515\">&quot;i&quot;<\/span>)\n        Console.WriteLine(<span style=\"color: #a31515\">&quot;j is a complex number:&quot;<\/span>)\n        Console.WriteLine(NumberPrinter2(j))\n        <span style=\"color: green\">'You can also use the short-circuiting feature of the if operator\n        <\/span><span style=\"color: blue\">Dim <\/span>k <span style=\"color: blue\">As <\/span>Number\n        Console.WriteLine(<span style=\"color: #a31515\">&quot;k does not have any value, so we print j again:&quot;<\/span>)\n        Console.WriteLine(<span style=\"color: blue\">If<\/span>(k <span style=\"color: blue\">Is Nothing<\/span>, j.ToString, k.ToString))\n        k = <span style=\"color: blue\">New <\/span>Number(3, 4)\n        Console.WriteLine(<span style=\"color: #a31515\">&quot;And now for k with a value:&quot;<\/span>)\n        Console.WriteLine(<span style=\"color: blue\">If<\/span>(k <span style=\"color: blue\">Is Nothing<\/span>, j.ToString, k.ToString))\n    <span style=\"color: blue\">End Sub\nEnd Module\n<\/span><\/pre>\n<p><a href=\"http:\/\/11011.net\/software\/vspaste\"><\/a><a href=\"http:\/\/11011.net\/software\/vspaste\"><\/a><\/p>\n<p>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.<\/p>\n<p>For those addicted IIF users wondering if they can still use the legacy IIF function in Orcas, the answer is <strong>yes. <\/strong>We still support IIF, but with all the amazing things that you can do with the <strong>IF operator, <\/strong>why would you want to keep on typing those extra &quot;i&quot;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,<\/p>\n<pre class=\"code\"><span style=\"color: blue\">Dim <\/span>blog_next = <span style=\"color: blue\">If<\/span>(bIFoundMoreInterestingScenarios, blogDelvingIntoIF, blogUnknown)<\/pre>\n<p>If you know what I mean \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":260,"featured_media":8818,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[192,195],"tags":[142,166],"class_list":["post-4623","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-featured","category-visual-basic","tag-sophia-salim","tag-vb2008"],"acf":[],"blog_post_summary":"<p>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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/4623","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/users\/260"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/comments?post=4623"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/4623\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media\/8818"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media?parent=4623"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/categories?post=4623"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/tags?post=4623"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}