This is my second attempt and for some reason won't Calculate. I get $0.00 as my result. VB tells me something is wrong wth CalcMiscCharge missing a return statement, but don't see where. Any Ideas?

Public Class Form1
    Private decLabFee As Decimal ' To hold Lab fee.
    Private decMeds As Decimal  'To hold Medication cost.
    Private decSurgical As Decimal 'To hold Surgical cost.
    Private intLengthOfStay As Integer 'Amount of days stayed at hospital.
    Private decPhycical As Decimal 'Hold Amount of Physical Cost.


    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Me.Close()

    End Sub



    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

        Dim StayCharge As Decimal
        Dim MiscCharge As Decimal
        Dim TotalCost As Decimal

        StayCharge = CalcStayCharge(txtLengthOfStay.Text)

        MiscCharge = CalcMiscCharges(txtMeds.Text, txtSurgical.Text, txtLabFee.Text, txtPhysical.Text)

        TotalCost = CalcTotalCharges(StayCharge, MiscCharge)

        lblTotalCost.Text = FormatCurrency(TotalCost)



    End Sub

    Function CalcStayCharge(ByVal txtLengthOfStay As String) As Decimal

        'Calcuate Stay Charges.
        Dim StayCharge As Decimal
        Dim NumDays As Decimal
        Try
            NumDays = txtLengthOfStay

        Catch ex As Exception
            MessageBox.Show("All Charges Must Be Zero or More", "CHECK INPUT!")
        End Try
        If NumDays < 0 Then
            MessageBox.Show("Length of Stay Can Not be less than Zero", "Negative Number ERROR!")
            Return 0
        End If
        StayCharge = NumDays * 350
        Return StayCharge
    End Function


    Function CalcMiscCharges(ByVal Meds As Decimal, ByVal LabFee As Decimal,
                             ByVal Physical As Decimal, ByVal Surgical As Decimal) As Decimal

        Dim MiscCharges As Decimal

        Try
            Meds = CDec(txtMeds.Text)
            Surgical = CDec(txtSurgical.Text)
            Physical = CDec(txtPhysical.Text)
            LabFee = CDec(txtLabFee.Text)

        Catch ex As Exception
            MessageBox.Show("All Charges Must Be Zero or More", "Negative Number ERROR!")
            GoTo ReDo
        End Try

        If Meds < 0 Or Surgical < 0 Or LabFee < 0 Or Physical < 0 Then
            MessageBox.Show("You May not enter a Minus Charge", "Negative Chare ERROR!")
            GoTo ReDo
        End If


        MiscCharges = Meds + Surgical + LabFee + Physical
        Return MiscCharges
ReDo:
    End Function

    Function CalcTotalCharges(ByVal StayCharge As Decimal, ByVal MiscCharge As Decimal) As Decimal
        Dim TotalCost As Decimal
        Dim StayCharges As Decimal
        Dim MiscCharges As Decimal

        TotalCost = StayCharges + MiscCharges
        CalcTotalCharges = CDec(TotalCost.ToString)
        Return CalcTotalCharges + MiscCharges
    End Function

    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        'Clears all text boxes

        txtLengthOfStay.Clear()

        txtMeds.Clear()

        txtSurgical.Clear()

        txtLabFee.Clear()

        txtPhysical.Clear()

        'Clear label boxes

        lblTotalCost.Text = String.Empty

    End Sub


End Class

Basically get rid of the Redo: and the goto's,they indicate sloppy programming anyway, and replace them with return statements. Since the return type is Decimal, you'll have to figure out what number you want to use to signify an error. -1 might do the trick.

On a side note, rather than using a Try-Catch block for common errors, it's usually more efficient to use the TryParse method of the Double class. Another option would be to use a NumericUpDown control, which can take typed input as well as scrolling input.

Edited 2 Years Ago by tinstaafl

I agree with tinstaafl, re-write your code to get rid of the "GoTo" statements and use TryParse instead. Also you may consider using "OrElse" instead of "Or".

(OrElse and Or) and (AndAlso and And) - When to use?

Issue: ...something is wrong wth CalcMiscCharge missing a return statement...

Resolution: It is because of your use of the GoTo statements. There are situations where you may GoTo "ReDo:". When this occurs, there is no return statement. To fix that problem, you could do the following:

ReDo:

Return MiscCharges

However, as stated above, you really should re-write your code to get rid of the GoTo statements--they are seldom necessary. Also, using them will cause you problems when you switch to a language that doesn't have them.

Edited 2 Years Ago by cgeier

0.00 is the right answer for you because too many wrong codes you write.

1) In btnCalculate.Click event you already passed the argument values in several functions. But in the code area of every functions you tried to initialize the parameters. And you did it wrongly though this is a wrong process.
**Meds = CDec(txtMeds.Text)
Surgical = CDec(txtSurgical.Text)
Physical = CDec(txtPhysical.Text)
LabFee = CDec(txtLabFee.Text)
**

2) Did not convert string to decimal.
**'Calcuate Stay Charges.
NumDays = txtLengthOfStay
**

GoTo Redo is wrong expression.
Use here "Return 0"

I have made some modification in your code. Hope, you could get your desired result.

  Private Sub btnCalculate_Click(sender As System.Object, e As System.EventArgs) Handles btnCalculate.Click
        Dim StayCharge As Decimal
        Dim MiscCharge As Decimal
        Dim TotalCost As Decimal
        StayCharge = CalcStayCharge(txtLengthOfStay.Text)
        MiscCharge = CalcMiscCharges(CDec(Val(txtMeds.Text)), CDec(Val(txtSurgical.Text)), CDec(Val(txtLabFee.Text)), CDec(Val(txtPhysical.Text)))
        TotalCost = CalcTotalCharges(StayCharge, MiscCharge)
        lblTotalCost.Text = FormatCurrency(TotalCost)

    End Sub

    Function CalcStayCharge(ByVal txtLengthOfStay As String) As Decimal
        'Calcuate Stay Charges.
        Dim StayCharge As Decimal
        Dim NumDays As Decimal
        Try
            NumDays = txtLengthOfStay
        Catch ex As Exception
            MessageBox.Show("All Charges Must Be Zero or More", "CHECK INPUT!")
            Return 0
        End Try
        If NumDays < 0 Then
            MessageBox.Show("Length of Stay Can Not be less than Zero", "Negative Number ERROR!")
            Return 0
        End If
        StayCharge = NumDays * 350
        Return StayCharge
    End Function

    Function CalcMiscCharges(ByVal Meds As Decimal, ByVal LabFee As Decimal,
                             ByVal Physical As Decimal, ByVal Surgical As Decimal) As Decimal
        Dim MiscCharges As Decimal
        If Meds < 0 Or Surgical < 0 Or LabFee < 0 Or Physical < 0 Then
            MessageBox.Show("You May not enter a Minus Charge", "Negative Chare ERROR!")
            Return 0
        End If
        MiscCharges = Meds + Surgical + LabFee + Physical
        Return MiscCharges

    End Function

    Function CalcTotalCharges(ByVal StayCharge As Decimal, ByVal MiscCharge As Decimal) As Decimal
        Dim TotalCost As Decimal

        TotalCost = StayCharge + MiscCharge

        Return TotalCost
    End Function

Edited 2 Years Ago by Santanu Das

This article has been dead for over six months. Start a new discussion instead.