Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
VB
' Local variable declarations
        Dim dblLengthOfStay As Double           ' To hold the number of days in the hospital
        Dim dblMedicationCharges As Double      ' To hold the medication charges
        Dim dblSurgicalCharges As Double        ' To hold the surgical charges
        Dim dblLabFees As Double                ' To hold the lab fees
        Dim dblPhysicalRehabCharges As Double   ' To hold the physical rehabilitation charges
        Dim dblStayCharges As Double            ' To hold the base charges for the hospital stay
        Dim dblMiscCharges As Double            ' To hold the total of miscelaneous charges
        Dim dblTotalCharges As Double           ' To hold the total charges

        ' Get the length of stay as input from the user.
        ' Verify that the value is a positive real number.
        If Double.TryParse(txtLengthOfStay.Text, dblLengthOfStay) And
            dblLengthOfStay >= 0.0 Then

            ' Get the medication charges as input from the user.
            ' Verify that the value is a positive real number.
            If Double.TryParse(txtMedicationCharges.Text, dblMedicationCharges) And
                dblMedicationCharges >= 0.0 Then

                ' Get the surgical charges as input from the user.
                ' Verify that the value is a positive real number.
                If Double.TryParse(txtSurgicalCharges.Text, dblSurgicalCharges) And
                    dblSurgicalCharges >= 0.0 Then

                    ' Get the lab fees as input from the user.
                    ' Verify that the value is a positive real number.
                    If Double.TryParse(txtLabFees.Text, dblLabFees) And
                        dblLabFees >= 0.0 Then

                        ' Get the physical rehabilitation charges as input from the user.
                        ' Verify that the value is a positive real number.
                        If Double.TryParse(txtPhysicalRehabCharges.Text, dblPhysicalRehabCharges) And
                            dblPhysicalRehabCharges >= 0.0 Then

                            ' Call the CalcStayCharges function and store the result.
                            dblStayCharges = CalcStayCharges(dblLengthOfStay, dblCHARGE_PER_DAY)

                            ' Call the CalcMiscCharges function and store the result.
                            dblMiscCharges = CalcMiscCharges(dblMedicationCharges, dblSurgicalCharges,
                                                             dblLabFees, dblPhysicalRehabCharges)

                            ' Call the CalcTotalCharges function and store the result.
                            dblTotalCharges = CalcTotalCharges(dblStayCharges, dblMiscCharges)

                            ' Display the total charges as output to the user.
                            lblTotalCharges.Text = dblTotalCharges.ToString("c")
                        Else
                            ' Display a message to the user indicating that the value for the physical
                            ' rehabilitation charges is invalid.
                            MessageBox.Show("The physical rehabilitation charges must " &
                                            "be a positive real number.", "Invalid Input")
                        End If
                    Else
                        ' Display a message to the user indicating that the value for the lab fees is invalid.
                        MessageBox.Show("The lab fees must be a positive real number.", "Invalid Input")
                    End If
                Else
                    ' Display a message to the user indicating that the value for the surgical charges is invalid.
                    MessageBox.Show("The surgical charges must be a positive real number.", "Invalid Input")
                End If
            Else
                ' Display a message to the user indicating that the value for the medication charges is invalid.
                MessageBox.Show("The medication charges must be a positive real number.", "Invalid Input")
            End If
        Else
            ' Display a message to the user indicating that the value for the length of stay is invalid.
            MessageBox.Show("The length of stay must be a positive real number.", "Invalid Input")
        End If
    End Sub
Posted
Updated 26-Apr-15 19:38pm
v2
Comments
Suvendu Shekhar Giri 27-Apr-15 1:37am    
Don't put all your code in the project and don't ask for "better code". Nobody is here to do code review of your complete project.
Question 1

1 solution

I wouldn't nest the conditions, and I wouldn't but the error code in the Else condition: I'd reverse the conditions and report the error as the If part.
VB
Dim dblLengthOfStay As Double
If Not Double.TryParse(txtLengthOfStay.Text, dblLengthOfStay) OrElse dblLengthOfStay < 0.0 Then
   MessageBox.Show("The length of stay must be a positive real number.", "Invalid Input")
   Return
End If
And repeat the blocks until all values are checked.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900