0

beginner visual basic coder- icnldued all the code if needed, but decTotalCharges = ComputeCharges(intDays, decMCharges, decSCharges, decLabFees, decRehabFees) is giving me a hard time - An error shows that says "Argument not specified for parameter 'decRehabFees' of "Public Function Compute Changes....." Any idea how to fix this? Or any tips to simplify this code? It's for a college project lol.

Option Strict 
Public Class Form1
   Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
    'Declare variables
    Dim intDays As Integer
    Dim decMCharges As Decimal
    Dim decSCharges As Decimal
    Dim decLabFees As Decimal
    Dim decRehabFees As Decimal
    Dim decTotalCharges As Decimal
    Dim decTax As Decimal
    Dim decTotal As Decimal

    Try
        GetInfo(intDays, decMCharges, decSCharges, decLabFees, decRehabFees)
        decTotalCharges = ComputeCharges(intDays, decMCharges, decSCharges, decLabFees, decRehabFees)
        decTax = ComputeTax(decTotalCharges, decTax)
        decTotal = ComputeTotal(decTotalCharges, decTax, decTotal)
        Display(decTotalCharges, decTax, decTotal)
    Catch ex As Exception

    End Try
End Sub
Sub GetInfo(ByRef intDays As Integer, ByRef decMCharges As Decimal, ByRef decSCharges As Decimal, ByRef decLabFees As Decimal, ByRef decRehabFees As Decimal)
    'Recieves values
    intDays = CInt(txtDays.Text)
    decMCharges = CDec(txtMCharges.Text)
    decSCharges = CDec(txtSCharges.Text)
    decLabFees = CDec(txtLabFees.Text)
    decRehabFees = CDec(txtRehabFees.Text)
End Sub
Function ComputeCharges(ByRef decTotalCharges As Decimal, ByVal intDays As Integer, ByVal decMCharges As Decimal, ByVal decSCharges As Decimal, ByVal decLabFees As Decimal, ByVal decRehabFees As Decimal) As Decimal
    'GetInfo(intDays, decMCharges, decSCharges, decLabFees, decRehabFees)
    decTotalCharges = CDec(intDays * 300) + decMCharges + decSCharges + decLabFees + decRehabFees
End Function
Function ComputeTax(ByVal decTotalCharges As Decimal, ByRef decTax As Decimal) As Decimal
    decTax = CDec(decTotalCharges * 0.07)
End Function
Function ComputeTotal(ByVal decTotalCharges As Decimal, ByVal decTax As Decimal, ByRef decTotal As Decimal) As Decimal
    decTotal = decTotalCharges + decTax
End Function
Function Display(ByVal decTotalCharges As Decimal, ByVal decTax As Decimal, ByVal decTotal As Decimal) As String
    lblTax.Text = decTax.ToString("c")
    lblTotal.Text = decTotal.ToString("c")
    lblTotalCharges.Text = decTotalCharges.ToString("c")
End Function

End Class

3
  • Several of your methods are poorly implemented. For example, ComputeTax is declared as a Function but it doesn't return anything, although you are using a single output parameter. That function should be declared Function ComputeTax(decTotalCharges As Decimal) As Decimal and then implement Return decTotalCharges * 0.07D and then called decTax = ComputeTax(decTotalCharges). Several other methods should be changed similarly. Commented Jun 16, 2020 at 3:24
  • When deciding how to declare methods, you need to think about whether there is output, how many output items there are and whether multiple items are equivalent. No output means a Sub and a single output means a Function, both with no ByRef parameters. If there are multiple outputs then you'll need at least one ByRef parameter. If there is one "primary" output then use a Function and return that, using ByRef parameters for the rest, e.g. Integer.TryParse. If there is no single "primary" output, use a Sub and all ByRef parameters. Commented Jun 16, 2020 at 3:28
  • Have you been specifically instructed to use ByRef by your course tutor? It's presence is making your code a convoluted mess and it should be stripped out completely. We have ways of returning multiple values from functions (Write your own class, or use a ValueTuple). ByRef should nearly never be used; you've used it more in this code block than I have in the last 10 years of commercial coding as a day job Commented Jun 16, 2020 at 5:47

2 Answers 2

2

ComputeCharges expects 6 arguments. You only feed it 5. You need to provide the first argument, ByRef decTotalCharges As Decimal.

Sign up to request clarification or add additional context in comments.

1 Comment

No, actually I think that's just treating the symptoms, not the cause
0

You did a good job making single job methods but I think you went a bit overboard. If your Function only has single line of code then there isn't much point in passing around ByRef parameters. I would like to see the validation code in the Validating event of the textboxes. Don't depend on exception handling to handle preventable exceptions.

.TryParse returns a Boolean and fills in the variable provided with the correct datatype.

When you do use Functions in .net, they need a return type and a Return statement that matches the type of the function.

Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
    'Validate user input
    Dim intDays As Integer
    If Not Integer.TryParse(txtDays.Text, intDays) Then
        MessageBox.Show("Fill in Days with a number")
        Exit Sub
    End If
    Dim decMCharges As Decimal
    If Not Decimal.TryParse(txtMCharges.Text, decMCharges) Then
        MessageBox.Show("Fill in MCharges with a number")
        Exit Sub
    End If
    Dim decSCharges As Decimal
    If Not Decimal.TryParse(txtSCharges.Text, decSCharges) Then
        MessageBox.Show("Fill in SCharges with a number")
        Exit Sub
    End If
    Dim decLabFees As Decimal
    If Not Decimal.TryParse(txtLabFees.Text, decLabFees) Then
        MessageBox.Show("Fill in LabFees with a number")
        Exit Sub
    End If
    Dim decRehabFees As Decimal
    If Not Decimal.TryParse(txtRehabFees.Text, decRehabFees) Then
        MessageBox.Show("Fill in RehabFees with a number")
        Exit Sub
    End If
    'Compute charges
    Dim decTotalCharges As Decimal
    Dim decTax As Decimal
    Dim decTotal As Decimal
    decTotalCharges = intDays * 300 + decMCharges + decSCharges + decLabFees + decRehabFees
    decTax = decTotalCharges * 0.07D
    decTotal = decTotalCharges + decTax
    'Display
    lblTax.Text = decTax.ToString("c")
    lblTotal.Text = decTotal.ToString("c")
    lblTotalCharges.Text = decTotalCharges.ToString("c")
End Sub

1 Comment

Thanks Mary-had to follow specific instructions, though I do like the .TryParse function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.