Trending September 2023 # Select Case Statement Inwith Examples # Suggested October 2023 # Top 14 Popular | Uyenanhthammy.com

Trending September 2023 # Select Case Statement Inwith Examples # Suggested October 2023 # Top 14 Popular

You are reading the article Select Case Statement Inwith Examples updated in September 2023 on the website Uyenanhthammy.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Select Case Statement Inwith Examples

What is Select Case in VB.Net?

Select Case is a conditional statement, that helps you test a variable for equality against a set of values. Each value is referred to as a case, and a variable that is being switched on should be checked for all the select cases.

The Select Case statement provides you with an easy way of testing for the contents of a variable. However, it is only suitable for use when a variable in question has only a limited number of options.

In this VB Net tutorial, you will learn:

Syntax of Select Case Statement in VB.Net

The Select Case statement is declared in chúng tôi using following syntax:

Select [ Case ] your_expression [ Case expression_list [ statement(s) ] ] [ Case Else [ else statement(s) ] ] End Select

Let us describe the parameters used in the syntax:

your_expression: this denotes an expression which evaluates to one of the elementary Data Types supported in Microsoft VB.NET.

expression_list: expression clauses that denote the match values for the expression. For the case of multiple clauses, separate them using a comma (,).

statement(s): statements that follow the Case and they execute after the select expression has matched any clause in expression_list.

else statements: statements that follow the Case Else and run once the select expression fails to match any of the clauses in the expression_list for any Case statement.

VB.Net Select Case Statement Program Examples

Let us use an example to demonstrate how to use Select Case Statement in VB.Net:

Step 1) First, create a new console application.

Step 2) Use the following code:

Module Module1 Sub Main() Dim name As String name = "Guru99" Select Case name Case "John" Console.WriteLine("Hello John") Case "Guru99" Console.WriteLine("Hello Guru99") Case "Alice" Console.WriteLine("Hello Alice") Case "Joel" Console.WriteLine("Hello Joel") Case Else Console.WriteLine("unknown name") End Select Console.WriteLine("VB.NET is easy!") Console.ReadKey() End Sub End Module

Here is a screenshot of the code that we have used:

Explanation of Code:

Creating a module named Module1.

Start of the main sub-procedure.

Creating a string variable named name.

Assigning a value of Guru99 to the variable name.

The value of a variable name will be used for performing comparisons with the various Case statements to find a match.

If the value of a variable name is John.

Text to print on the console if the above Case is true/matched.

If the value of a variable name is Guru99.

Text to print on the console if the above Case is true/matched.

If the value of a variable name is Alice.

Text to print on the console if the above Case is true/matched.

If the value of a variable name is Joel.

Text to print on the console if the above Case is true/matched.

If none of the above Case statements is true/ is matched.

Text to print on the console if the above Case is true, that is, no Case statement is matched.

End of the Select statement.

This statement will print some text on the screen regardless of whether a Case statement was matched or not. It will always execute.

Pause the console window for a while waiting for a user to take action to close it.

End of the main sub-procedure.

End of the module.

Example 2

You can also allow the user to type the name you make your decision based on that.

Step 1) Create a new console application.

Step 2) Use the following code:

Module Module1 Sub Main() Console.Write("Enter your name: ") Dim name As String = Console.ReadLine() Select Case name Case "John" Console.WriteLine("Hello John") Case "Guru99" Console.WriteLine("Hello Guru99") Case "Alice" Console.WriteLine("Hello Alice") Case "Joel" Console.WriteLine("Hello Joel") Case Else Console.WriteLine("unknown name") End Select Console.WriteLine("VB.NET is easy!") Console.ReadKey() End Sub End Module

Step 4) Type the name Guru99 and press the enter key. You should get the following:

Here is a screenshot of the code:

Explanation of Code:

Creating a module named Module1.

Start of the main sub-procedure.

Printing some text on the console instructing the user to enter their name.

Creating a string variable named name and prompting the user to enter a value for this variable on the console.

The value of the variable name will be used for performing comparisons with the various Case statements to find a match.

If the value of the variable name is John.

Text to print on the console if the above Case is true/matched.

If the value of the variable name is Guru99.

Text to print on the console if the above Case is true/matched.

If the value of the variable name is Alice.

Text to print on the console if the above Case is true/matched.

If the value of the variable name is Joel.

Text to print on the console if the above Case is true/matched.

If none of the above Case statements is true/ is matched.

Text to print on the console if the above Case is true, that is, no Case statement is matched.

End of the Select statement.

This statement will print some text on the screen regardless of whether a Case statement was matched or not. It will always execute.

Pause the console window for a while waiting for a user to take action to close it.

End of the main sub-procedure.

End of the module.

ToLower() and ToUpper() Functions in VB.Net

The Select Case statement is case sensitive. This means that it will treat guru99 as different from Guru99. However, we can use the ToLower() and the ToUpper() functions to handle the issue of a case with this statement.

Example of ToLower() and ToUpper() in VB.Net

Here is an example of ToLower() and ToUpper() function in VB.Net:

Step 1) Create a new console application.

Step 2) Use the following code:

Module Module1 Sub Main() Console.Write("Enter your name: ") Dim name As String = Console.ReadLine() Select Case name.ToLower() Case "john." Console.WriteLine("Hello John") Case "guru99." Console.WriteLine("Hello Guru99") Case "alice." Console.WriteLine("Hello Alice") Case "joel." Console.WriteLine("Hello Joel") Case Else Console.WriteLine("unknown name") End Select Console.WriteLine("VB.NET is easy!") Console.ReadKey() End Sub End Module

Step 4) Type the name Guru99 (G is uppercase) and press the enter key on your keyboard. You should get the following:

Here is a screenshot of the code:

Explanation of Code:

Code line 5: The value of the variable name will be used for performing comparisons with the various Case statements to find a match. The ToLower() function will ensure that any name the user types is first converted into lowercase before the evaluation of Case statements. This means that if the user types Guru99, it will be immediately converted to guru99, then the evaluation of the Case statements is done. If the user types John, it will be immediately be converted to john before the evaluation of the Case statements is done.

Rest of the code same as above.

Summary

The Select Case statement provided by chúng tôi helps you evaluate a value against a set of values through matching.

It is only suitable if the possible values of the variable in question is known to be limited.

The Select Case statement is case sensitive.

You can use the ToLower() and the ToUpper() functions to convert strings into lowercase and uppercase respectively.

You're reading Select Case Statement Inwith Examples

Update the detailed information about Select Case Statement Inwith Examples on the Uyenanhthammy.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!