Home » VB.NET Basics » 06 - Conditional Logic
6

Select Case

Working with Select Case statements

The Select Case statements are similar to the If-Then statements. The Select Case statements are used for executing different command lines in several different circumstances. For example, you can run a specific set of commands in scenario one, a specific set of commands in scenario two, and another set of commands in scenario three. Look at the code below and see how the Select Case logic works.

	
Sub ()
	Dim x As Integer
        Dim result As String

        x = 0
        result = "no value"

        Select Case x
            Case Is < 0
                result = "x is a negative integer"
            Case Is = 0
                result = "x is neither a positive nor a negative integer"
            Case Is > 0
                result = "x is a positive integer"
        End Select

        MsgBox(result)
		
End Sub

In the code above, you can see that the value for x can have three possible outcomes in the message box. Try changing the values of x to -4 and then to 9.

The basic structure of a Select Case code is as follows

	
	Select Case [variable]
		Case [1]
			[command lines]
		Case [2]
			[command lines]
		.
		.
		.
		Case [N]
			[command lines]
	End Select