Home » VB.NET » The VB.NET Ternary Operator

The VB.NET Ternary Operator

I think the VB.NET Ternary Operator may be the last operator that I really miss in VB.NET 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 
            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


Other post in VB.NET

Related Post

  • Object Initialization in CSharp 3.0 and VB.NET 9Object Initialization in CSharp 3.0 and VB.NET 9 Yesterday we looked at the new var keyword in CSharp.  This makes CSharp variable declaration similar to VB.  After all, they've had the DIM keyword for years which essentially does the […]
  • Assign Multiple enum Values To One VariableAssign Multiple enum Values To One Variable I saw this question and immediately thought, "You can't!  An Enum is an Integer that has been restricted to the values it can accept." And I was basically right.  But, […]
  • CSharp adds the var keyword!CSharp adds the var keyword! There have been several new features added to the CSharp language that will significantly reduce the amount of code that ends up in our source files.  It will not significantly reduce the […]
  • Simple Properties in C# 3.5Simple Properties in C# 3.5 It's such a little thing.  But, how much of our CSharp code looks something like this: private string _propertyName; public string PropertyName { get { […]
  • Advantages of Using Class DiagramAdvantages of Using Class Diagram One of the new tools that showed up in Visual Studio 2005 that I don't see many people taking much advantage of is the Class Diagram. The class diagram displays the classes you drag […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer focusing on ASP.NET, C#, Node.js, JavaScript, HTML, CSS, BootStrap, and Angular.JS. Does your team need additional help in any of the above? Contact Dave today.

Leave a Reply

6 Comments on "The VB.NET Ternary Operator"

Notify of
Sort by:   newest | oldest | most voted
Tern Aryfa IL

horrible example that leads developers to write ugly unmaintainable code.

I like the first example

Dave Bush

I’m not sure what you are trying to express. What are you referring to as a “horrible example that leads developers to write ugly unmaintainable code”? The statement, itself is an opinion. What makes the code ugly? What makes it unmaintainable?

Tyree Jackson

I have to respectfully disagree. I far more prefer the second example over the first. It’s much clearer that an assignment is being performed and that no other action is being conducted based on the condition. With the first example I have to read 4 lines to be sure that nothing other than as assignment is being performed.

Tern Aryfa IL
your second example takes a fraction of a second longer to read/understand than the first. I get where you’re going with it but the example was a poor one. someone scanning your example quickly would read a line that had if( _ on one line. and stuff on a second line. Because the second and third parameters in your ternary operation are not simple, there is a level of effort that a person has to apply; they have to (1)interpret that it’s a ternary operation and (2)read into the line to find the else condition. For that example, using the… Read more »
Dave Bush
If I understand you correctly, your chief complaint is that it is on two lines instead of one. If I were writing code for a program instead of writing code for a blog post, I would have placed it all on one line. Unfortunately, putting it all on one line would have made it too wide to display correctly. In defense of the example itself, I would be more likely to use this syntax to check for nothing prior to using the value as I have done in this example then I would be to return one of two values… Read more »
Tern Aryfa IL
good points but i think you’re off the mark on my position. It’s not so much the two lines as it is the type of parameters you’re using for the second and third positions. I guess it comes down to this: “black”,”white” vs String.Empty, Session(“mySessionVar”).ToString() when the parameters get as complicated as your example array index lookup + function call, it makes it harder to distinguish the two parts of the statement without having to do more than a quick scan with your eyes. It has nothing to do with being familiar with the ternary pattern you’re using for the… Read more »