|
-
Dec 4th, 2009, 12:49 AM
#1
Thread Starter
New Member
Hospital Charges form
Trying to figure out why exactly it will display the total value of decMiscCharges + decCharges. Any advice would be appreciated.
vb.net Code:
Option Strict On
Option Explicit On
Public Class Form1
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculateCharges.Click
'declare all variables
Dim decCharge As Decimal
Dim intLengthOfStay As Integer
Dim decMedCharges As Decimal
Dim decSurgicalCharge As Decimal
Dim decDayRate As Decimal
Dim decMiscCharge As Decimal
Dim decSurgicalCharges As Decimal
Dim decTotalCharge As Decimal
Dim decMeds As Decimal
Dim decLabFee As Decimal
If isValid(decMeds, decSurgicalCharge, intLengthOfStay, decLabFee) = True Then
decCharge = CalcStayCharge(intLengthOfStay, decDayRate)
decMiscCharge = CalcMiscCharges(decMedCharges, decSurgicalCharges, decLabFee)
decTotalCharge = CalcTotalCharge(decMiscCharge, decCharge)
txtTotalCost.Text = decTotalCharge.ToString("c2")
End If
End Sub
Private Function isValid(ByRef decMeds As Decimal, ByRef decSurgeicalCharge As Decimal, _
ByRef intLengthOfStay As Integer, ByRef decLabFee As Decimal) As Boolean
Return isValidStay(intLengthOfStay) AndAlso isValidMedication(decMeds) _
AndAlso isValidSurgical(decSurgeicalCharge) AndAlso CBool(isValidLabFee(decLabFee))
Return isValid
End Function
Private Function isValidStay(ByRef intLengthOfStay As Integer) As Boolean
'Declare Boolean
Dim isGood As Boolean
'Declare Constants
Const c_DaysError As String = "Length of stay must be a whole number between 0 and 100"
Try
intLengthOfStay = Convert.ToInt32(txtLengthOfDays.Text)
If intLengthOfStay >= 0 And intLengthOfStay <= 100 Then
isGood = True
Else
isGood = False
End If
Catch ex As Exception
isGood = False
End Try
If isGood = False Then
MessageBox.Show(c_DaysError)
txtLengthOfDays.SelectAll()
txtLengthOfDays.Focus()
End If
Return isGood
End Function
Public Function isValidLabFee(ByVal decLabFee As Decimal) As Decimal
'Declare Variable
Dim isGood As Boolean
'Declare Constant
Const c_LabFeeError As String = "Medication must be between 0 and 10,000"
Try
decLabFee = Convert.ToDecimal(txtLabFees.Text)
If decLabFee >= 0D And decLabFee <= 10000D Then
isGood = True
Else
isGood = False
End If
Catch ex As Exception
isGood = False
MessageBox.Show(c_LabFeeError)
txtLabFees.SelectAll()
txtLabFees.Focus()
End Try
Return CDec(isGood)
End Function
Public Function isValidMedication(ByVal decMedCharges As Decimal) As Boolean
'Declare Variable
Dim isGood As Boolean
'Declare Constant
Const c_MedicationError As String = "Medication must be between 0 and 10,000"
Try
decMedCharges = Convert.ToDecimal(txtMedicationCharges.Text)
If decMedCharges >= 0D And decMedCharges <= 10000D Then
isGood = True
Else
isGood = False
End If
Catch ex As Exception
isGood = False
MessageBox.Show(c_MedicationError)
txtMedicationCharges.SelectAll()
txtMedicationCharges.Focus()
End Try
Return isGood
End Function
Public Function isValidSurgical(ByVal decSurgicalCharges As Decimal) _
As Boolean
'Declare Variables
Dim isGood As Boolean
'Declare constants
Const c_SurgicalError As String = "Surgical must be between 0 and 500,000"
Try
decSurgicalCharges = Convert.ToDecimal(txtSurgicalCharges.Text)
If decSurgicalCharges >= 0D And decSurgicalCharges <= 500000D Then
isGood = True
Else
isGood = False
End If
Catch ex As Exception
isGood = False
MessageBox.Show(c_SurgicalError)
txtSurgicalCharges.SelectAll()
txtSurgicalCharges.Focus()
End Try
Return isGood
End Function
Private Sub txtLabFees_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtLabFees.TextChanged
End Sub
End Class
Module modTotalCharge
Public Function CalcTotalCharge(ByRef decMiscCharge As Decimal, ByRef decCharge As Decimal) _
As Decimal
Return decMiscCharge + decCharge
End Function
End Module
Module modMiscCharge
Function CalcMiscCharges(ByRef decMedCharges As Decimal, ByRef decSurgicalCharges As Decimal, ByRef decLabFee As Decimal) _
As Decimal
Return decMedCharges + decSurgicalCharges + decLabFee
End Function
End Module
Module modStayCharge
Public Function CalcStayCharge(ByVal intLengthofStay As Integer, ByVal decDayRate As Decimal) _
As Decimal
decDayRate = 350D
Return intLengthofStay * decDayRate
End Function
End Module
Last edited by Hack; Dec 4th, 2009 at 06:51 AM.
Reason: Added Highlight Tags
-
Dec 4th, 2009, 06:52 AM
#2
Re: Hospital Charges form
-
Dec 4th, 2009, 08:13 AM
#3
Hyperactive Member
Re: Hospital Charges form
Maybe because you have a function called CalcTotalCharge that adds the two
Code:
Public Function CalcTotalCharge(ByRef decMiscCharge As Decimal, ByRef decCharge As Decimal) _
As Decimal
Return decMiscCharge + decCharge
End Function
and then you doing this :
Code:
decTotalCharge = CalcTotalCharge(decMiscCharge, decCharge)
txtTotalCost.Text = decTotalCharge.ToString("c2")
Is this what you meant ?
-
Dec 4th, 2009, 02:54 PM
#4
Thread Starter
New Member
Re: Hospital Charges form
 Originally Posted by Kyonn
Trying to figure out why exactly it will display the total value of decMiscCharges + decCharges. Any advice would be appreciated.
vb.net Code:
Option Strict On
Option Explicit On
Public Class Form1
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculateCharges.Click
'declare all variables
Dim decCharge As Decimal
Dim intLengthOfStay As Integer
Dim decMedCharges As Decimal
Dim decSurgicalCharge As Decimal
Dim decDayRate As Decimal
Dim decMiscCharge As Decimal
Dim decSurgicalCharges As Decimal
Dim decTotalCharge As Decimal
Dim decMeds As Decimal
Dim decLabFee As Decimal
If isValid(decMeds, decSurgicalCharge, intLengthOfStay, decLabFee) = True Then
decCharge = CalcStayCharge(intLengthOfStay, decDayRate)
decMiscCharge = CalcMiscCharges(decMedCharges, decSurgicalCharges, decLabFee)
decTotalCharge = CalcTotalCharge(decMiscCharge, decCharge)
txtTotalCost.Text = decTotalCharge.ToString("c2")
End If
End Sub
Private Function isValid(ByRef decMeds As Decimal, ByRef decSurgeicalCharge As Decimal, _
ByRef intLengthOfStay As Integer, ByRef decLabFee As Decimal) As Boolean
Return isValidStay(intLengthOfStay) AndAlso isValidMedication(decMeds) _
AndAlso isValidSurgical(decSurgeicalCharge) AndAlso CBool(isValidLabFee(decLabFee))
Return isValid
End Function
Private Function isValidStay(ByRef intLengthOfStay As Integer) As Boolean
'Declare Boolean
Dim isGood As Boolean
'Declare Constants
Const c_DaysError As String = "Length of stay must be a whole number between 0 and 100"
Try
intLengthOfStay = Convert.ToInt32(txtLengthOfDays.Text)
If intLengthOfStay >= 0 And intLengthOfStay <= 100 Then
isGood = True
Else
isGood = False
End If
Catch ex As Exception
isGood = False
End Try
If isGood = False Then
MessageBox.Show(c_DaysError)
txtLengthOfDays.SelectAll()
txtLengthOfDays.Focus()
End If
Return isGood
End Function
Public Function isValidLabFee(ByVal decLabFee As Decimal) As Decimal
'Declare Variable
Dim isGood As Boolean
'Declare Constant
Const c_LabFeeError As String = "Medication must be between 0 and 10,000"
Try
decLabFee = Convert.ToDecimal(txtLabFees.Text)
If decLabFee >= 0D And decLabFee <= 10000D Then
isGood = True
Else
isGood = False
End If
Catch ex As Exception
isGood = False
MessageBox.Show(c_LabFeeError)
txtLabFees.SelectAll()
txtLabFees.Focus()
End Try
Return CDec(isGood)
End Function
Public Function isValidMedication(ByVal decMedCharges As Decimal) As Boolean
'Declare Variable
Dim isGood As Boolean
'Declare Constant
Const c_MedicationError As String = "Medication must be between 0 and 10,000"
Try
decMedCharges = Convert.ToDecimal(txtMedicationCharges.Text)
If decMedCharges >= 0D And decMedCharges <= 10000D Then
isGood = True
Else
isGood = False
End If
Catch ex As Exception
isGood = False
MessageBox.Show(c_MedicationError)
txtMedicationCharges.SelectAll()
txtMedicationCharges.Focus()
End Try
Return isGood
End Function
Public Function isValidSurgical(ByVal decSurgicalCharges As Decimal) _
As Boolean
'Declare Variables
Dim isGood As Boolean
'Declare constants
Const c_SurgicalError As String = "Surgical must be between 0 and 500,000"
Try
decSurgicalCharges = Convert.ToDecimal(txtSurgicalCharges.Text)
If decSurgicalCharges >= 0D And decSurgicalCharges <= 500000D Then
isGood = True
Else
isGood = False
End If
Catch ex As Exception
isGood = False
MessageBox.Show(c_SurgicalError)
txtSurgicalCharges.SelectAll()
txtSurgicalCharges.Focus()
End Try
Return isGood
End Function
Private Sub txtLabFees_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtLabFees.TextChanged
End Sub
End Class
Module modTotalCharge
Public Function CalcTotalCharge(ByRef decMiscCharge As Decimal, ByRef decCharge As Decimal) _
As Decimal
Return decMiscCharge + decCharge
End Function
End Module
Module modMiscCharge
Function CalcMiscCharges(ByRef decMedCharges As Decimal, ByRef decSurgicalCharges As Decimal, ByRef decLabFee As Decimal) _
As Decimal
Return decMedCharges + decSurgicalCharges + decLabFee
End Function
End Module
Module modStayCharge
Public Function CalcStayCharge(ByVal intLengthofStay As Integer, ByVal decDayRate As Decimal) _
As Decimal
decDayRate = 350D
Return intLengthofStay * decDayRate
End Function
End Module
It will total up the LengthOfStay, but not the decMiscCharges
-
Dec 4th, 2009, 11:59 PM
#5
Thread Starter
New Member
Re: Hospital Charges form
Any idea why it won't add the lblLabFees lblSurgicalFees and lblMedicationFees?
-
Dec 5th, 2009, 12:27 AM
#6
Re: Hospital Charges form
Well first off, you don't add strings together. Secondly, I don't even see in your code where lblLabFees, lblSurgicalFees and lblMedicationFees, I see them with the txt in front of it. Thirdly, to add objects that are currently a string, you need to convert them to Double or Decimal.
To do that, you use a Double.TryParse() or a Decimal.TryParse() and assign it to a value.
Example:
vb.net Code:
Dim Str As String = "3" Dim Dbl As Double = Double.NaN If Double.TryParse(Str, Dbl) = False Then 'Error Else 'Parsed OK End If
Once you parse then all, then you can add the double values together and then convert it back to a string. The reason you want to do the TryParse instead of something simpler like CDbl() or CDec() is what if somehow someone made txtLabFees.text = "h4x!". The moment you try to do a CDbl() or a CDec(), the program will crash. If you do a TryParse, you can catch that error earlier and handle it much cleaner than usual.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|