VB.NET vs C# - If/Then/Else Syntax

Posted Posted 2/5/2010 5:14:34 PM

Does VB.NET's if-then-else syntax bother the crap out of anyone else out there?



  1.         If isCondition AndAlso isOtherCondition Then
  2.             DoSomething()
  3.         ElseIf isCondition Then
  4.             DoSomethingElse()
  5.         ElseIf isOtherCondition Then
  6.             DoSomethingElseElse()
  7.         End If
  8.  






Wow, that's verbose and wasteful for a simple operation like that, don't ya think? Fortunately, VB.NET still carries over the : operator from the decades of yore, and we can abbreviate it thusly:



  1.         If isCondition AndAlso isOtherCondition Then
  2.             DoSomething()
  3.         ElseIf isCondition Then : DoSomethingElse()
  4.         ElseIf isOtherCondition Then : DoSomethingElseElse() : End If
  5.  






Still needs work. Just for comparison, let's see what this is like in C#:



  1.         if (isCondition && isOtherCondition) DoSomething();
  2.         else if (isCondition) DoSomethingElse();
  3.         else if (isOtherCondition) DoSomethingElseElse();
  4.  






Aaaaahhhhhh, that's better.

And don't even get me started on the ?? and ?: operators. Come on VB, you need these things!



Project Codename MV8
Copyright © 2010 Kevin Connolly. All rights reserved.
Your request ate 82 of my milliseconds.