Public Class Form1
'first i'll input the calculate stay charges function
Function CalculateStay(ByVal days As Decimal) As Decimal
'hold the charges value
Dim charges As Decimal
'calculation of charges
charges = days * 350
Return charges
End Function
'now enter the MISC function
Function CalculateMisc(ByVal medical As Decimal,
ByVal surgical As Decimal,
ByVal Lab As Decimal,
ByVal physical As Decimal) As Decimal
'hold misc charges
Dim TotalMisc As Decimal
'calculation
TotalMisc = medical + surgical + Lab + physical
Return TotalMisc
End Function
'now enter Overall charges function
Function CalculateOverall(ByVal charges As Decimal,
ByVal TotalMisc As Decimal) As Decimal
Dim OverallCost As Decimal
OverallCost = charges + TotalMisc
Return OverallCost
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
Application.Exit()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
Dim currentTextBox As TextBox
'Loop through all the top level controls
For Each ctrl As Control In Me.Controls
'If the control is a TextBox do the next action.
If TypeOf ctrl Is TextBox Then
'Converted the type of ctrl to a TextBox and assign
'it to "currentTextBox"
currentTextBox = CType(ctrl, TextBox)
'Cleared the current TextBox.
currentTextBox.Clear()
End If
Next
End Sub
Private Sub btnCalculateStay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
'hold days,medication,surgical,lab, physical and total cost value
Dim decDays As Decimal
Dim decMedical As Decimal
Dim decSurgical As Decimal
Dim decLab As Decimal
Dim decPhysical As Decimal
Dim decOverallCost As Decimal
Dim charges As Decimal
Dim TotalMisc As Decimal
'do the conversions
decDays = CDec(txtStay.Text)
decMedical = CDec(txtMed.Text)
decSurgical = CDec(txtSurgeryCharge.Text)
decLab = CDec(txtLab.Text)
decPhysical = CDec(txtPhysical.Text)
'use if statement to make sure no values are negative
If decDays >= 0 And decMedical >= 0 And decSurgical >= 0 And decLab >= 0 And decPhysical >= 0 Then
charges = CalculateStay(decDays)
TotalMisc = CalculateMisc(decMedical, decSurgical, decLab, decPhysical)
decOverallCost = CalculateOverall(charges, TotalMisc)
lblTotalCost.Text = decOverallCost.ToString("n2")
Else
MessageBox.Show("The values should be numeric")
End If
End Sub
End Class
6 Replies - 256 Views - Last Post: 12 October 2012 - 02:31 PM
#1
question with how to make my 3 functions more efficient
Posted 12 October 2012 - 01:06 PM
Hey, I'm trying to complete a problem using functions however I feel that there is a better way to do this. In essence i created a form application that calculates the total cost of a hospital stay. I HAVE to ue 3 functions so this is the way I went about doint it. I'm catching some errors towards the end of the code but I feel like there is a shorter more efficient way to do this. AFter all isn't the purpose of using functions to shorten the code? your opinions would be helpful.
Replies To: question with how to make my 3 functions more efficient
#2
Re: question with how to make my 3 functions more efficient
Posted 12 October 2012 - 01:44 PM
There's really no rocket science in those 3 functions. Simple addition and multiplication, so there's really not much of optimization waiting to be done there (after all, your question is about functions, and their efficiency). Design-vise you could create reusable object, that could be used with any UI independently.
#3
Re: question with how to make my 3 functions more efficient
Posted 12 October 2012 - 01:50 PM
I agree, but I can offer this bit of information where you're clearing your textboxes.
Since ctrl is a control that you end up casting to textbox after you check that it is of type textbox, there's no reason to create another control of type textbox to set it to. You can just use
Since ctrl is a control that you end up casting to textbox after you check that it is of type textbox, there's no reason to create another control of type textbox to set it to. You can just use
For each ctrl as Controls in Me.Controls
If TypeOf ctrl Is Textbox Then
Ctype(ctrl, Textbox).Clear()
End If
Next
#4
Re: question with how to make my 3 functions more efficient
Posted 12 October 2012 - 02:06 PM
lucky, I'm new to VB so not exactly sure what you mean by that comment. The assignment asks us to create the form and use at least 3 functions or subroutines so that's why i'm doing it this way. the book is a bit convulted for me so this is what I pieced together but it seems like an awful lot to go through for simple multiplication and addition.
#5
Re: question with how to make my 3 functions more efficient
Posted 12 October 2012 - 02:26 PM
Well, as you said it, you needed to create at least 3 methods (functions or subroutines), and you did it. Your original question was how to make those 3 more efficient. I don't see how you can perform addition more efficiently, when you're summing 3 variables. You can do it many different ways, but that doesn't mean it will be more or less efficient. On top of what CharlieMay proposed, you can do it without if also (and there are other ways too):
But there's no point in this. The point of assignment is using methods, and you did it. In your OP, you mention some errors towards the end of the code. What are they?
For Each ctrl As TextBox In Me.Controls.OfType(Of TextBox)()
ctrl.Clear()
Next
But there's no point in this. The point of assignment is using methods, and you did it. In your OP, you mention some errors towards the end of the code. What are they?
#6
Re: question with how to make my 3 functions more efficient
Posted 12 October 2012 - 02:28 PM
well there's no errors flagged but when i run the code and input values and hit the calculate button, nothing happens
#7
Re: question with how to make my 3 functions more efficient
Posted 12 October 2012 - 02:31 PM
That's something else. You have to ask yourself, what would you like to do with those calculations. Would you like to show them as MessageBox, or print them, save them to database, file... You decide. Then try to do it, and if you'll have problems, you can count on us to guide you and provide help.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|