1 Private Sub btnCalculate_Click(ByVal sender As System.Object, _ 2 ByVal e As System.EventArgs) Handles btnCalculate.Click 3 4 Dim decSales() As Decimal = Nothing ' Sales array 5 Dim decTotal As Decimal ' Total sales 6 Dim decAverage As Decimal ' Average sales 7 Dim decHighest As Decimal ' Highest sales 8 Dim decLowest As Decimal ' Lowest sales 9 10 If GetSalesData(decSales) Then 11 ' Calculate total, average, highest, 12 ' and lowest sales 13 decTotal = GetTotal(decSales) 14 decAverage = GetAverage(decSales) 15 decHighest = GetHighest(decSales) 16 decLowest = GetLowest(decSales) 17 18 ' Display the results. 19 lblTotal.Text = FormatCurrency(decTotal) 20 lblAverage.Text = FormatCurrency(decAverage) 21 lblHighest.Text = FormatCurrency(decHighest) 22 lblLowest.Text = FormatCurrency(decLowest) 23 End If 24 End Sub 25 26 27 Function GetSalesData(ByRef p_decSales() As Decimal) As Boolean 28 29 ' Prompts the user for sales figures and stores the 30 ' values in an array. Returns True if the procedure 31 ' was successful, otherwise returns False. 32 33 Dim decSalesData() As Decimal ' Sales data array 34 Dim intNumDays As Integer = 0 ' Number of days 35 Dim intCount As Integer ' Loop counter 36 Dim blnSuccess As Boolean ' Indicates success or failure 37 38 Try 39 intNumDays = CInt(InputBox("For how many days do you " _ 40 & "have sales?")) 41 Catch 42 MessageBox.Show("You entered a nonnumeric value", _ 43 "Error") 44 End Try 45 46 If intNumDays > 0 Then 47 ' Resize the array to the correct number of days. 48 ReDim decSalesData(intNumDays - 1) 49 50 ' Input each day's sales from the user. 51 For intCount = 0 To (intNumDays - 1) 52 decSalesData(intCount) = CDec(InputBox( _ 53 "Enter the sales for " & _ 54 "day " & (intCount + 1).ToString)) 55 Next 56 57 ' Assign the array to the parameter variable. 58 p_decSales = decSalesData 59 blnSuccess = True 60 Else 61 MessageBox.Show("You must enter at least one day " _ 62 & "of sales.") 63 blnSuccess = False 64 End If 65 Return blnSuccess 66 End Function 67 68 Function GetTotal(ByVal p_decValues() As Decimal) As Decimal 69 70 ' Calculate and return the total of the 71 ' values in the array argument. 72 73 Dim decTotal As Decimal = 0 ' Accumulator 74 Dim intCount As Integer ' Loop counter 75 76 For intCount = 0 To (p_decValues.Length - 1) 77 decTotal += p_decValues(intCount) 78 Next 79 Return decTotal 80 End Function 81 82 Function GetAverage(ByVal p_decValues() As Decimal) As Decimal 83 ' Calculate and return the average of the 84 ' values in the array argument. 85 86 Return GetTotal(p_decValues) / p_decValues.Length 87 End Function 88 89 Function GetHighest(ByVal p_decValues() As Decimal) As Decimal 90 91 ' Returns the largest value in a Decimal array. 92 Dim intCount As Integer ' Loop counter 93 Dim decHighest As Decimal = p_decValues(0) ' Largest value 94 95 For intCount = 1 To (p_decValues.Length - 1) 96 If p_decValues(intCount) > decHighest Then 97 decHighest = p_decValues(intCount) 98 End If 99 Next 100 Return decHighest 101 End Function 102 103 Function GetLowest(ByVal p_decValues() As Decimal) As Decimal 104 105 ' Returns the smallest value in a Decimal array. 106 Dim decCount As Integer ' Loop counter 107 Dim decLowest As Decimal = p_decValues(0) ' Smallest value 108 109 For decCount = 1 To (p_decValues.Length - 1) 110 If p_decValues(decCount) < decLowest Then 111 decLowest = p_decValues(decCount) 112 End If 113 Next 114 Return decLowest 115 End Function 116 117 Private Sub btnExit_Click(ByVal sender As System.Object, _ 118 ByVal e As System.EventArgs) Handles btnExit.Click 119 ' End the applicaiton 120 121 Me.Close() 122 End Sub