DevOps
20 Best TeamViewer Alternative in 2021 [Free/Paid]
TeamViewer is a remote desktop software that allows you to connect to multiple workstations...
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:
The Select Case statement takes the following syntax:
Select [ Case ] your_expression
[ Case expression_list
[ statement(s) ] ]
[ Case Else
[ else statement(s) ] ]
End Select
Let us describe the parameters used above:
Let us use an example to demonstrate how to use this statement.
Step 1) First, create a new console application.
Step 2) Use the following code:
Module Module1
Sub Main()
Dim name As String
name = "gtupapers"
Select Case name
Case "John"
Console.WriteLine("Hello John")
Case "gtupapers"
Console.WriteLine("Hello gtupapers")
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 3) Click the Start button from the top bar to execute the program. It will give you the following result:
Here is a screenshot of the code that we have used:
Explanation of Code:
You can also allow the user to type the name you make your decision based on that. For example:
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 "gtupapers"
Console.WriteLine("Hello gtupapers")
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 3) Click the Start button from the top bar to execute it. It should return the following:
Step 4) Type the name gtupapers and press the enter key. You should get the following:
Here is a screenshot of the code:
Explanation of Code:
The Select Case statement is case sensitive. This means that it will treat gtupapers as different from gtupapers. However, we can use the ToLower() and the ToUpper() functions to handle the issue of a case with this statement.
For example:
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 "gtupapers."
Console.WriteLine("Hello gtupapers")
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 3) Click the Start button on the top bar to run the code. You should get the following output:
Step 4) Type the name gtupapers (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 gtupapers, it will be immediately converted to gtupapers, 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.
TeamViewer is a remote desktop software that allows you to connect to multiple workstations...
Photo viewer is computer software that can display stored pictures. These tools can handle many...
External hard disks are a convenient way of storing data. Portable SSD external drives enable you...
Download PDF 1) Mention what is Jenkins? Jenkins is an open source tool with plugin built for...
Notepad++ is an open source code editor written in C++. It supports various programming languages...
In this tutorial, you will learn Simple Linear regression Multiple Linear regression Continuous...