Search code examples
arraysvb.netparameter-passingsubroutine

Getting numbers from an array into a subroutine that contains a calculation in VB


So I need to have the console application ask the user for names, hours, and payrate. These answers will be stored in appropriate arrays. I need to use the information from the hours and payrate arrays in a calculation contained in a subroutine. I tried to find some sort of hint via my textbook or online but I can't seem to find anything that helps me in this situation...

I dont understand the error I am getting on the following lines.

total = overtimePay(hours(i), rate(i))
total = regularPay(hours(i), rate(i))

the error i get is:

"Value of type 'Double' cannot be converted into a '1-dimentional array of Double'

my program works completely when i just put the calculation equations here instead of the subroutines

Here is the entirety of my code.

Module FinalAssignment1


Sub Main()


    'Author: Russell Peryy
    'Date: 4/2/16
    'Purpose: User enters info and program outputs the entered pay and calculated info

    'Declare constants
    Const author As String = "Russell Perry =================== Final Assignment 1"
    Const lines As String = "===================================================="

    'Declare Arrays
    Dim names(0 To 10) As String
    Dim hours(0 To 10) As Double
    Dim rate(0 To 10) As Double
    'Dim total(0 To 10) As Double


    'Declare variabels
    Dim i As Integer = 0
    Dim total As Double = 0

    'Display constants
    Console.WriteLine(author)
    Console.WriteLine(lines)
    space(1)

    'Get user information to fill name, hours, and rate array
    For i = 0 To 9 Step 1
        Console.Write("Enter employee's last name >> ")
        names(i) = Console.ReadLine()

        Console.Write("Enter employee's hours worked >> ")
        hours(i) = Console.ReadLine()

        Console.Write("Enter the employee's pay rate >> ")
        rate(i) = Console.ReadLine()

        'i = i + 1
        space(1)
    Next

    space(1)
    Console.WriteLine(lines)

    'Print info to screen
    For i = 0 To 9 Step 1
        If hours(i) >= 40 Then
            total = overtimePay(hours(i), rate(i))
            'total = (40 * rate(i)) + ((hours(i) - 40) * rate(i) * 1.5)
        Else
            total = regularPay(hours(i), rate(i))
            'total = hours(i) * rate(i)
        End If

        Console.WriteLine(names(i) & " worked " & hours(i) & " at a rate of " & String.Format("{0:C}", rate(i)) & " an hour for a total pay of " & String.Format("{0:C}", total))

    Next i

    'Pause the screen
    space(1)
    Console.WriteLine(lines)
    space(1)
    Console.Write("Press any key to exit >> ")
    Console.ReadKey()

End Sub

'subroutine for adding spaces
Sub space(ByVal x As Integer)
    For counter = 0 To x
        Console.WriteLine()
    Next
End Sub

'Subroutine for regular pay
Sub regularPay(ByVal array1() As Double, ByVal array2() As Double, ByVal i As Integer)
    Dim t As Double = array1(i) * array2(i)
End Sub

'subroutine for overtime
Sub overtimePay(ByVal array1() As Double, ByVal array2() As Double, ByVal i As Integer)
    Dim total As Double = ((40 * array2(i)) + ((array1(i) - 40) * array2(i) * 1.5))
End Sub
End Module

Solution

  • When you call the functions, the parameters aren't the array, they are a double inside the array. So your two functions should not have the parameters "ByVal array2() as double", but instead should just be "hours as double" (for array1) and "rate as double". At that point, you won't need the i index into the array, since you aren't passing the array.

    Then in the one line inside the functions, don't use "array2(i)", just use "rate", and instead of "array1(i)" just use your parameter name "hours"