Home | Education | Learn A Language
I think this may be the last operator that I really miss in VB from my curly brace language experience. Although, I have to admit, I wouldn’t have missed it all that much if they never added it. There just isn’t a whole lot of use for it. However, the Ternary operator is a REALLY nice feature to have available to you when you do need it. It’s another one of those language features that falls under, “Just because it is there doesn’t mean you have to use it.” If you’ve ever run into a situation where you just need a simple evaluation and assign a variable based on it. Like this: Dim s As String If Session("mySessionVar") Is Nothing Then s = String.Empty Else s = Session("mySessionVar").ToString() End If you’ll appreciate the new Ternary operator which shrinks it to: Dim s As String s = If(Session("mySessionVar") Is Nothing, _ String.Empty, Session("mySessionVar").ToString) Note that this NOT the same as Dim s As String s = IIf(Session("mySessionVar") Is Nothing, _ String.Empty, Session("mySessionVar").ToString) Here’s the difference between the two. IIf will always evaluate the second and third parameter regardless of if the first parameter evaluates to true or false. This is because IIf is a function, not an operator. If is an operator, and therefore only evaluates the second OR third parameter when they are the value that will ultimately be returned. So, If() will run my code above without any errors while IIf will throw a null pointer exception when Session(”mySessionVar”) evaluates to nothing because it will try to apply ToString() to the object that is null. Technorati Tags: vb.net,visual studio del.icio.us Tags: vb.net,visual studio 2008
Free Article Content Directory: http://www.articlefair.com
About The Author Dave answers ASP.NET related questions at .NET Answers and runs a DotNetNuke Web Hosting company at My Win Hosting.
Please Rate this Article
5 out of 54 out of 53 out of 52 out of 51 out of 5
Not yet Rated
Join Our Newsletter To Discover New Articles and Chances To Win Hot Products!
Article Fair copyrighted. www.ArticleFair.com All rights protected.