Select....Case Statement in VB.NET

Select....Case Statement

The Select Case statement executes one of several groups of statements depending on the value of an expression. If your code has the capability to handle different values of a particular variable then you can use a Select Case statement. You use Select Case to test an expression, determine which of the given cases it matches and execute the code in that matched case.

The syntax of the Select Case statement looks like this:

Select Case testexpression
[Case expressionlist-n
[statements-n]] . . .
[Case Else elsestatements]
End Select

Example

Imports System.Console
Module Module1

Sub Main()
Dim keyIn As Integer
WriteLine("Enter a number between 1 and 4")
keyIn = Val(ReadLine())

Select Case keyIn
Case 1
WriteLine("You entered 1")
Case 2
WriteLine("You entered 2")
Case 3
WriteLine("You entered 3")
Case 4
WriteLine("You entered 4")
End Select

End Sub

End Module

No comments: