Select Case and If-Then-Else can be used for exactly the same programming problems. It's up to you, as a programmer, to decide which one works best.
Consider this code (used in a popular Microsoft Step by Step text from years past - the numbers are a little dated now):
Dim AdjustedIncome, TaxDue As Double If AdjustedIncome <= 27050 Then ' 15% tax bracket TaxDue = AdjustedIncome * 0.15 ElseIf AdjustedIncome <= 65550 Then ' 28% tax bracket TaxDue = 4057.5 + ((AdjustedIncome - 27050) * 0.28) ElseIf AdjustedIncome <= 136750 Then ' 31% tax bracket TaxDue = 4057.5 + ((AdjustedIncome - 65550) * 0.31) ElseIf AdjustedIncome <= 297350 Then ' 36% tax bracket TaxDue = 4057.5 + ((AdjustedIncome - 136750) * 0.36) Else ' 39.6% tax bracket TaxDue = 4057.5 + ((AdjustedIncome - 297350) * 0.396) End If
Whenever you have a series of decisions like this, a Select Case structure can also be used. Here's the same code using a Select Case Structure:
Select Case AdjustedIncome Case Is <= 27050 ' 15% tax bracket TaxDue = AdjustedIncome * 0.15 Case Is <= 65550 ' 28% tax bracket TaxDue = 4057.5 + ((AdjustedIncome - 27050) * 0.28) Case Is <= 136750 ' 31% tax bracket TaxDue = 4057.5 + ((AdjustedIncome - 65550) * 0.31) Case Is <= 297350 ' 36% tax bracket TaxDue = 4057.5 + ((AdjustedIncome - 136750) * 0.36) Case Else ' 39.6% tax bracket TaxDue = 4057.5 + ((AdjustedIncome - 297350) * 0.396) End Select
If you initialize AdjustedIncome to say, $1,000,000 (that is, a small, insignificant fraction of what Bill Gates makes in a year), you will find that you get exactly the same answer with either code.
So ... Which One Should You Use?
Fundamentally, it's a matter of your preferred programming style. The bottom line is getting the program to work correctly and efficiently and both styles do that. But my preferred programming style is Select Case. I find it easier to read and understand than the If structure. To my mind, it's easier to see exactly which statements are executed for every condition.
Frequent About Visual Basic contributor, Peter Zilahy Ingerman adds this reasoning, "Select Case makes writing multiple Or conditions simpler, so that if one begins by having a zillion separate Case statements, and one discovers that there are really only three or four actions, collapsing the Cases under a Select Case is a *whole* lot easier than rewriting the If-ElseIf-...-End structure."
From a performance point of view, there isn't any reason to choose one over the other. I checked using a StopWatch object to time the two examples shown above. Even using ten million random iterations didn't make the time necessary to execute them vary by more than a few hundred milliseconds. As a final check, I looked at the intermediate language (the language that all .NET code is compiled into before execution) for a Release build of both routines. They were almost identical.
Click Here to display the illustration
The lesson here is that even if you do like one coding style over another, the VB.NET compiler will create the most efficient code possible.