VB6 Tutorial 19: Select Case

Posted On 2018-04-06 by VB6Boy
Keywords:
Tags: VB6 Tutorials Windows
Views: 43

Title Uploaded Size
WordLength.zip 4/7/2018 5:17:00 AM 1,156
NumberLength.zip 4/7/2018 5:17:15 AM 1,201
GradeProgram.zip 4/7/2018 5:17:31 AM 1,275


Looking for the more of the VB6 Tutorial? Here is the tutorial index.

This lesson takes you through the Select Case block, a very useful decision making structure in Visual Basic.

The Select Case block provides an efficient way for decision making which is used to execute one block of statement(s) among a list of statement blocks according to the determined value of an expression. The expression is preceded by the "Select Case" phrase. The choices are made based on the value of this expression.

Syntax

 

Select Case expression
    Case value0
        statements
    Case value1
        statements
    Case value2
        statements
        ...........
        ...............
    Case else
        statements
End select

 

Example

 

Dim num As Integer
num = Val(Text1.Text)
Select Case num
Case 0
    Print "You have entered zero"
Case 1
    Print "You have entered one"
Case 2
    Print "You have entered two"
Case Else
    Print "The number you entered is greater than 2"
End Select

 

One block of statement(s) will be executed depending upon the value of num. If num=0 then, Case 0 will be evaluated and if num =1 then Case 1 will be evaluated.

See the other forms of Select Case Structure in the following programs.

Example

 

Dim score As Integer
score = Val(Text1.Text)
Select Case score
Case 900 To 1000
    Print "First position"
Case 800 To 899
    Print "Second position"
Case 700 To 799
    Print "Third position"
Case Else
    Print "No position, try again"
End Select

 

Example

 

Dim num As Integer
num = Val(Text1.Text)
Select Case num
Case Is > 0
    Print "Positive number"
Case Is < 0
    Print "Negative number"
Case 0
    Print "Zero"
End Select

 

Example

 

Select Case value
    Case 0, 1, 2, 3, 4, 5
        Print "The values are 0,1,2,3,4,5"
    Case Else
        Print "Other values"
End Select

 

Use the attached sample programs to see basic examples of Select ... Case in action.

One of the programs (Numbers) uses a function called Val(). We haven't learned about that one yet, but it is coming in a future lesson. In a nutshell, it converts the numbers in a string variable to a numeric value.

Looking for the more of the VB6 Tutorial? Here is the tutorial index.


About the Author

VB6Boy has posted a total of 71 articles.


Comments On This Post

By: CrazyDude2
Date: 2018-04-07

I'd also like to point out that using Select Case has a short-circuited quality when using Comma Separated (or) for testing inputs.  For example for that "Case 0, 1, 2, 3, 4, 5", if the input value is 2, it will stop there and not try to evaluate the 3, 4 and 5.  But in a line like "If value = 0 Or value = 1 Or value = 2 Or value = 3 Or value = 4 Or value = 5 Then", VB will try every one of them even if it sees that 3 is the matching value.  So Select Case can be very useful as a more efficient replacement for an "all Or" If/Then line of code.


Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.


Your IP address is:23.20.165.182

Before you can post, you need to prove you are human. If you log in, this test goes away.




Code Links