Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use CASE statement in VB.NET

Shulou Source: shulou.com Published: 2022-06-02 08:57:14 09月10日 Update

Editor to share with you how to use the CASE sentence in VB.NET. I hope you will get something after reading this article. Let's discuss it together.

If you want to compare the same expression with different values, you can replace the If...Then...Else statement with the Select...Case statement. The difference is that the If and ElseIf statements evaluate different expressions in each statement, while the Select statement evaluates a single expression only once and then compares it with a different value.

Let's look at an example. The code is as follows:

Function bonus (ByVal performance As Integer, _ ByVal salary As Decimal) As Decimal Select performance Case 1 Return salary * 0.1 Case 2 Return salary * 0.3 Case 3 Return salary * 0.7 Case 4 Return salary * 0.9 Case 5 Return salary * 1.2 End Select End Function

This function (Function) for calculating employee bonuses (bonus) has two parameters, one is performance, the employee's performance, and the other is salary, which is the employee's salary. The performance of the staff has 5 grades, which is expressed as 1, 2, 3, 4, 5, respectively. This example uses the Select...Case statement, which means that when the employee's performance is 1, the employee's bonus is salary multiplied by 0.1; when the employee's performance is 2, the employee's bonus is salary times 0.3; when the employee's performance is 3, the employee's bonus is salary multiplied by 0.7; when the employee's performance is 4, the employee's bonus is salary times 0.9; when the employee's performance is 5, the employee's bonus is salary multiplied by 1.2.

Through this example, you can understand the structure of the Select...Case statement. As follows:

Select... (an expression, such as a variable) Case... (a value). (execute code) Case... (a value). Execute code End Select

You can use any number of VB.NET CASE statements to increase the value you want to compare.

Visual Basic compares the value of the expression with the value in the Case statement in the order in which it appears in the Select...Case structure. If the value of a Case matches, the corresponding code of the Case will be executed. After execution, no other VB.NET Case statements will be executed and will go directly to End Select.

VB.NET CASE Else statement

If the expression does not match any of the values of the Case statement, you can use the Case Else statement to handle the exception. For example, in the previous example, in addition to values from 1 to 5, what if the employee's performance is 0 or greater than 6?

You can rewrite the program as follows:

Function bonus (ByVal performance As Integer, _ ByVal salary As Decimal) As Decimal Select performance Case 1 Return salary * 0.1 Case 2 Return salary * 0.3 Case 3 Return salary * 0.7 Case 4 Return salary * 0.9 Case 5 Return salary * 1.2 Case Else Return 0 End Select End Function

Notice that the rewritten example adds two lines of code:

Case Else Return 0

These two lines of code mean that if performance is not equal to any value of the Case statement, the statement after Case Else is executed and returns 0.

VB.NET Case statements can contain multiple values and a range of values

Function bonus (ByVal performance As Integer, _ ByVal salary As Decimal) As Decimal Select performance Case 1 Return salary * 0.1 Case 2 To 3 Return salary * 0.3 Case 3 To 7 Return salary * 0.7 Case 8 To 9 Return salary * 0.9 Case Is

Tags: Statement employee code bonus salary expression example different multiple meaning article structure scope two function single parameter variable available in as above Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Docker Microsoft macOS Huawei