' Basic Programming ' Ch. 8 Demo Program #3 ' by Mr. Minich ' purpose - to illustrate an example of an event procedure calling a general ' procedure
Option Explicit Const MAX_CLASS_SIZE As Integer = 23 ' Only 23 students can be enrolled ' in the class since there are ' only 23 computers. ' ******* module-level variables ************ Dim mTestScores(1 To MAX_CLASS_SIZE) As Integer ' student test scores Dim mNumStudents As Integer ' total number of students Private Sub cmdAddTestScore_Click() ' Purpose: event procedure that prompts the user for a complete ' set of student test scores ' Precondition: None ' Postcondition: integer test scores will be placed into array of test scores ' Called By: none ' Calls: AddTestScore Call AddTestScore End Sub Private Sub cmdCalculateAverage_Click() ' Purpose: event procedure that calculates class test average ' Precondition: at least one score is entered into array, mTestScores, ' otherwise, division-by-zero error will occur ' Postcondition: class test average will be displayed in a label Dim Subtotal As Integer ' subtotal of all student test scores Dim ClassAverage As Single ' class test average Dim J As Integer ' loop variable For J = 1 To mNumStudents Subtotal = Subtotal + mTestScores(J) Next J ClassAverage = Subtotal / mNumStudents ' computing class average lblClassAverage.Caption = ClassAverage ' displaying class average End Sub Private Sub cmdDisplayScores_Click() ' Purpose: event procedure that displays the student test scores in a listbox ' Precondition: scores are entered into an array ' Postcondition: test scores will be placed into a listbox Dim J As Integer ' loop variable For J = 1 To mNumStudents lstDisplayedScores.AddItem mTestScores(J) Next J End Sub Private Sub AddTestScore() ' Purpose: event procedure that prompts the user for a complete ' set of student test scores ' Precondition: none ' Postcondition: integer test scores will be placed into array of test scores ' Called By: AddTestScore_Click ' Calls: None Dim J As Integer ' loop variable Dim ExitFlag As Integer ' flag variable to control user input Dim StudentNum As Integer ' keeps track of current student number Dim UserPrompt As String ' prompt for next student test score personalized ' with the current student number ' ***** Initializing local variables ***** ExitFlag = 0 ' set flag to zero so that the loop iterates StudentNum = 1 MsgBox ("Warning: Inputting new scores will overwrite previous scores!" & vbCr & _ "Enter a score of -1 to indicate that all scores have been entered.") ' -1 is a sentinel value to indicate the end of the user's input ' ***** Obtaining user input ************* Do UserPrompt = "Enter the test score for student number " & StudentNum & ": " mTestScores(StudentNum) = InputBox(UserPrompt) ' obtaining the next test score If mTestScores(StudentNum) = -1 Then mNumStudents = StudentNum - 1 ' set the total number of students ' subtracting one so that an extra ' student is not counted ExitFlag = 1 Else StudentNum = StudentNum + 1 ' increment to next student number End If Loop While (ExitFlag <> 1 And mNumStudents < MAX_CLASS_SIZE) End Sub