Select Case Statements - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Select Case Statements

Description:

Q. If we change the name of the variable SomeDay, how many changes will be ... outputs the names of a colour of the rainbow based on the following values... – PowerPoint PPT presentation

Number of Views:70
Avg rating:3.0/5.0
Slides: 10
Provided by: matthe221
Category:

less

Transcript and Presenter's Notes

Title: Select Case Statements


1
Select Case Statements
2
  • Consider the following If statement...
  • Dim SomeDay As String
  • Dim DayNo As Integer
  • SomeDay txtDayName.Text
  • If SomeDay "Sun" Then
  • DayNo 1
  • ElseIf SomeDay "Mon" Then
  • DayNo 2
  • ElseIf SomeDay "Tue" Then
  • DayNo 3
  • ElseIf SomeDay "Wed" Then
  • DayNo 4
  • ElseIf SomeDay "Thu" Then
  • DayNo 5
  • ElseIf SomeDay "Fri" Then
  • DayNo 6
  • ElseIf SomeDay "Sat" Then
  • DayNo 7

3
  • The code reads in a value from a text box called
    txtDayName, and assigns a numeric value to the
    variable DayNo depending on the text entered in
    the box. This numeric value is ultimately
    displayed via a text box caled txtDayNo.
  • The above If then ElseIf statement works fine for
    the task at hand, however the problem with these
    kinds of statements is that they can become
    unwieldy as their complexity increases.
  • Q. What would we need to do to the above code
    should we need to change the name of the variable
    SameDay?

4
A. We would need to change the following lines of
code.
  • Dim SomeDay As String
  • SomeDay txtDayName.Text
  • If SomeDay "Sun" Then
  • ElseIf SomeDay "Mon" Then
  • ElseIf SomeDay "Tue" Then
  • ElseIf SomeDay "Wed" Then
  • ElseIf SomeDay "Thu" Then
  • ElseIf SomeDay "Fri" Then
  • ElseIf SomeDay "Sat" Then

5
Here is the above code using a Select Case
statement...
  • Dim SomeDay As String
  • Dim DayNo As Integer
  • SomeDay txtDayName.Text
  • Select Case SomeDay
  • Case "Sun"
  • DayNo 1
  • Case "Sun"
  • DayNo 2
  • Case "Sun"
  • DayNo 3
  • Case "Sun"
  • DayNo 4
  • Case "Sun"
  • DayNo 5
  • Case "Sun"
  • DayNo 6
  • Case "Sun"
  • DayNo 7
  • Case Else

6
  • Notice that the Select Case statement starts with
    the words Select Case along with the name of the
    variable we want to test...
  • Select Case SomeDay
  • Unlike an If statement we do not insert any
    Boolean expressions at this point.

7
  • We then specify the tests (cases) that we require
    using the key word Case, e.g....
  • Case "Sun
  • DayNo 1
  • Case "Sun"
  • DayNo 2
  • Case "Sun"
  • DayNo 3
  • Case "Sun"
  • DayNo 4
  • Case "Sun"
  • DayNo 5
  • Case "Sun"
  • DayNo 6
  • Case "Sun"
  • DayNo 7
  • Case Else
  • DayNo 8
  • End Select

8
  • Q. If we change the name of the variable SomeDay,
    how many changes will be required in the above
    code?
  • A. We would need to change....
  • Dim SomeDay As String
  • SomeDay txtDayName.Text
  • Select Case SomeDay
  • And that 's it!

9
Sample Application
  • In order to see an example of a Select Case
    statement in action, we shall look at the
    creation of the following program....
  • The user enters a numeric value between 1 and 7,
    the application outputs the names of a colour of
    the rainbow based on the following values...
  • 1 Red
  • 2 Orange
  • 3 Yellow
  • 4 Green
  • 5 Blue
  • 6 Indigo
  • Violet
  • Should an invalid number be entered, than an
    error message should be displayed.
Write a Comment
User Comments (0)
About PowerShow.com