increase page rank

Home | Education | Learn A Language

The Ternary Operator in VB.NET

Bookmark and Share

By: Jim Schools

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

 

Not yet Rated

Click the XML Icon Above to Receive Learn A Language Articles Via RSS!




Join Our Newsletter To Discover New Articles
and
Chances To Win Hot Products!
 

First Name:
Email:






Article Fair copyrighted. www.ArticleFair.com   All rights protected.



Powered by Article Dashboard