Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use CASE statement in VB.NET

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report