OK, I’ve seen a lot of the VB guys complaining that using the ternary operator (which VB.NET just got as of 3.5) makes the code unclear. To which I’ve said said, and continue to say, “Yes of course, if used incorrectly.”
But, this post, over at ASP.NET without ASP.NET shows a pratical use of the ternary operator and shows that it can be a lot more clear than multiple if/else if statements.
Now, we all know that a case statement would be preferred to multiple if/else if statements WHEN THAT IS POSSIBLE. But, sometimes your if/else if is either not comparing against the same value, or is comparing multiple values. In both cases, a switch statement will not work, or be as clear as this example of the ternary operator.
Like this Article? Subscribe to get every article sent to your email.
- Two Interfaces, One Method, Two Meanings (VB) Last week I showed how to implement two interfaces with one method and two meanings in CSharp. One of the first comments I received for this post was, "How does this work in […]
- Intellisense Everywhere in VB 9 (and a small bug) One of the features the CSharp guys have had for quite a while is Intellisense everywhere. Want to create a private variable? Just start typing private and hit the tab key as soon as the […]
- AS is in CSharp too. Many CSharp programmers are surprised to find that CSharp has an AS keyword that looks remarkably similar to VB.NET's AS keyword. And while on the surface it looks similar to what AS does […]
- Assign 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, I forgot that […]
- 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 […]
About Dave Bush