19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

118 <strong>Computer</strong> <strong>Programming</strong> <strong>Concepts</strong> <strong>and</strong> <strong>Visual</strong> <strong>Basic</strong><br />

EXAMPLE 7<br />

Select Case is useful in defining functions that are not determined by a formula. The following program<br />

assumes the current year is not a leap year.<br />

Object Property Setting<br />

frm5_3_7 Caption Seasons<br />

lblSeason Caption Season<br />

txtSeason Text (blank)<br />

cmdNumber Caption Number of Days<br />

picNumDays<br />

Private Sub cmdNumber_Click()<br />

Dim season As String<br />

‘Determine the number of days in a season<br />

picNumDays.Cls<br />

season = txtSeason.Text<br />

picNumDays.Print season; “ has”; NumDays(season); “days.”<br />

End Sub<br />

Private Function NumDays(season As String) As Integer<br />

‘Look up the number of days in a given season<br />

Select Case UCase(season)<br />

Case “WINTER”<br />

NumDays = 87<br />

Case “SPRING”<br />

NumDays = 92<br />

Case “SUMMER”, “AUTUMN”, “FALL”<br />

NumDays = 93<br />

End Select<br />

End Function<br />

[Run, type Summer into the text box, <strong>and</strong> press the comm<strong>and</strong> button.]<br />

COMMENTS<br />

1. Some programming languages do not allow a value to appear in two different<br />

value lists; <strong>Visual</strong> <strong>Basic</strong> does. If the value of the selector appears in two different<br />

value lists, the action associated with the first value list will be carried out.<br />

2. In <strong>Visual</strong> <strong>Basic</strong>, if the value of the selector does not appear in any of the value<br />

lists <strong>and</strong> there is no Case Else clause, execution of the program will continue<br />

with the statement following the Select Case block.<br />

3. The Case statements <strong>and</strong> their actions do not have to be indented; however,<br />

because indenting improves the readability of the block, it is regarded as good<br />

programming style. As soon as you see the words Select Case, your eyes can<br />

easily scan down the block to find the matching End Select statement. You<br />

immediately know the number of different cases under consideration.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!