1 ' This application demonstrates parallel arrays. 2 3 ' Class-level Constants. 4 Const intNUMBER_OF_EMPLOYEES As Integer = 2 5 Const intMAX_EMPLOYEE As Integer = intNUMBER_OF_EMPLOYEES - 1 6 Const decHOURLY_PAY_RATE As Decimal = 6D 7 8 ' Class-level variables. 9 Private strNames(intMAX_EMPLOYEE) As String ' Employee names 10 Private intHours(intMAX_EMPLOYEE) As Integer ' Hours worked 11 Private decGrossPay(intMAX_EMPLOYEE) As Decimal ' Gross pay 12 Private decTotalGrossPay As Decimal ' Total gross pay 13 Private intTotalHours As Integer ' Total hours worked 14 15 Private Sub btnCalculate_Click(ByVal sender As System.Object, _ 16 ByVal e As System.EventArgs) Handles btnCalculate.Click 17 18 ' Call the Sub procedures to calculate 19 ' and display each employee's gross pay, the 20 ' total payroll, and the total labor hours worked. 21 22 GetData() 23 CalcPay() 24 DisplayPayrollData() 25 End Sub 26 27 Private Sub btnClear_Click(ByVal sender As System.Object, _ 28 ByVal e As System.EventArgs) Handles btnClear.Click 29 30 ' Clear the list box and labels 31 Dim intCount As Integer ' Loop counter. 32 33 ' Clear the list box 34 lstPayData.Items.Clear() 35 36 ' Clear labels for total payroll and labor hours. 37 lblTotalPayroll.Text = "" 38 lblLaborHours.Text = "" 39 40 ' Set the array elements to 0 41 For intCount = 0 To intMAX_EMPLOYEE 42 intHours(intCount) = 0 43 decGrossPay(intCount) = 0 44 Next intCount 45 46 ' Clear the totals. 47 decTotalGrossPay = 0 48 intTotalHours = 0 49 End Sub 50 51 Sub GetData() 52 ' Get the names of the employees and the 53 ' hours worked by each. 54 Dim intCount As Integer ' Loop counter 55 56 For intCount = 0 To intMAX_EMPLOYEE 57 strNames(intCount) = InputBox( _ 58 "Enter the name of employee " _ 59 & "number " & (intCount + 1).ToString, _ 60 "Employee Name") 61 62 intHours(intCount) = GetEmployeeHours(strNames(intCount)) 63 Next intCount 64 End Sub 65 66 Function GetEmployeeHours(ByVal strName As String) As Integer 67 68 ' Ask the user to enter the hours worked by a single 69 ' employee. If the value is not a valid integer, display 70 ' an error message and use a loop to ask again. 71 72 Dim intHours As Integer 73 Dim blnFinished As Boolean = False 74 Do Until blnFinished 75 Try 76 intHours = CInt(InputBox( _ 77 "Enter the hours worked by " _ 78 & strName, "Hours Worked")) 79 blnFinished = True 80 Catch 81 MessageBox.Show("Please enter an integer", "Error") 82 End Try 83 Loop 84 Return intHours 85 End Function 86 87 Sub CalcPay() 88 ' This procedure calculates and displays each 89 ' employee's gross pay. 90 91 Dim intCount As Integer ' Loop counter 92 Dim decGross As Decimal ' Gross pay 93 94 ' Calculate the gross pay for each person, the total 95 ' of all gross pay values, and the total hours worked. 96 97 For intCount = 0 To intMAX_EMPLOYEE 98 decGross = intHours(intCount) * decHOURLY_PAY_RATE 99 decGrossPay(intCount) = decGross 100 decTotalGrossPay += decGross 101 intTotalHours += intHours(intCount) 102 Next 103 End Sub 104 105 Sub DisplayPayrollData() 106 ' Displays the payroll data for each employee 107 ' and the total gross pay and total hours worked. 108 109 ' Display each employee's data. 110 Dim count As Integer 111 112 For count = 0 To intMAX_EMPLOYEE 113 lstPayData.Items.Add("Name: " & strNames(count) _ 114 & " Hours: " & intHours(count) & " Gross Pay: " _ 115 & FormatCurrency(decGrossPay(count))) 116 Next count 117 118 ' Display the total gross pay and total hours for 119 ' all employees. 120 lblTotalPayroll.Text = FormatCurrency(decTotalGrossPay) 121 lblLaborHours.Text = intTotalHours.ToString 122 End Sub 123 124 Private Sub btnExit_Click(ByVal sender As System.Object, _ 125 ByVal e As System.EventArgs) Handles btnExit.Click 126 127 ' End the application. 128 Me.Close() 129 End Sub