13 Replies - 743 Views - Last Post: 14 November 2011 - 08:29 PM Rate Topic: -----

#1 The_Chin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 13-November 11

Hospital Form Question Why Wont it Work?

Posted 13 November 2011 - 11:16 PM

 'Declare the price rate per night

    Const decNightly_Rate As Decimal = 350D

    Private Sub btnCalculateCharges_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculateCharges.Click
        'Declare variables

        Dim intNumberofDays As Decimal
        Dim decMiscCharges As Decimal
        Dim decTotal As Decimal
        Dim decMedCharges As Decimal
        Dim decLabFees As Decimal
        Dim decSurgicalCharges As Decimal
        Dim decPhysicalRehab As Decimal


        'Convert those variables

        intNumberofDays = CInt(txtLegnthofStay.Text)
        decMiscCharges = CDec(txtLabFees.Text)
        decMedCharges = CDec(txtMedicationCharges.Text)
        decLabFees = CDec(txtLabFees.Text)
        decSurgicalCharges = CDec(txtSurgicalCharges.Text)
        decPhysicalRehab = CDec(txtPhysicalRehabilitation.Text)

        'Call the procedures to be added together

        intNumberofDays = CalcStayCharges()
        decMiscCharges = decLabFees + decMedCharges + decPhysicalRehab + decSurgicalCharges
        decTotal = CalcTotalCharges()

        'Display the Result
        lblTotalCostDisplay.Text = decTotal.ToString

    End Sub

    Function CalcStayCharges() As Integer

        Dim intNumberofDays As Integer

        'Returns the amount for the number of nights stayed
        Return intNumberofDays * decNightly_Rate

    End Function

    Function CalcMiscCharges(ByVal decmedicationcharges As Decimal, ByVal decsurgicalcharges As Decimal, _
                              ByVal decphysicalrehabilitation As Decimal, ByVal declabfees As Decimal) As Decimal
        'this function calculates the misc charges
        Dim decMiscCharges As Decimal

        decMiscCharges = CDec(txtMedicationCharges.Text + txtLabFees.Text _
            + txtPhysicalRehabilitation.Text + txtSurgicalCharges.Text)

        Return decMiscCharges

    End Function

    Function CalcTotalCharges(ByRef decstaycharges As Decimal, ByRef decmisccharges As Decimal) As Decimal
        'this function calculates the total cost.
        
        CalcTotalCharges = decStayCharges.ToString + decMiscCharges.ToString

        Return CalcTotalCharges
    End Function

    Private Sub btnClaearForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClaearForm.Click
        txtLabFees.Text = Nothing
        txtLegnthofStay.Text = Nothing
        txtMedicationCharges.Text = Nothing
        txtPhysicalRehabilitation.Text = Nothing
        txtSurgicalCharges.Text = Nothing
    End Sub
End Class



I am trying to make a hospital form where the user inputs data for a hospital bill. I have to create three functions: CalcStayCharges, CalcMiscCharges, and CalcTotalCharges. I do not know why this code won't work. If someone could please help me on what to do, it would be much appreciated.

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Hospital Form Question Why Wont it Work?

#2 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 583
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Hospital Form Question Why Wont it Work?

Posted 14 November 2011 - 12:34 AM

You could set a global variable and then set it from within the functions, then set the value of the TextBox
Was This Post Helpful? 0
  • +
  • -

#3 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: Hospital Form Question Why Wont it Work?

Posted 14 November 2011 - 01:12 AM

It is not working because in every sub you have new declared variables. This means every sub will treat its variable in its own and their scope is bounded only to the respective subs.
To solve the problem you need to declare your variables as class variables, then you can use them in every sub without re declaring them again. Like what you did with decNightly_Rate, thus it can be used everywhere in a class.
But you declare intNumberofDays in every sub by its own, so the value entered in one sub wont affect the other sub. So in your code:
Function CalcStayCharges() As Integer

        Dim intNumberofDays As Integer ' you declare it here and so is default to 0. intNumberofDays = 0

        'Returns the amount for the number of nights stayed
        Return intNumberofDays * decNightly_Rate ' this will always return 0 because intNumberofDays is 0

    End Function

Was This Post Helpful? 0
  • +
  • -

#4 The_Chin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 13-November 11

Re: Hospital Form Question Why Wont it Work?

Posted 14 November 2011 - 10:20 AM

Ok I kinda get what you're saying. But don't functions default to Public so they can be used by the entire class? Anyways I keep getting this error thrown out in line 30 when I try to call CalcMiscCharges(), Error 1 Argument not specified for parameter 'decmisccharges' of 'Public Function CalcTotalCharges(ByRef decstaycharges As Decimal, ByRef decmisccharges As Decimal) As Decimal'. C:\Users\Cowboy\Desktop\Homework\HW2\Q3\Q3\Hospital Charges.vb
Was This Post Helpful? 0
  • +
  • -

#5 The_Chin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 13-November 11

Re: Hospital Form Question Why Wont it Work?

Posted 14 November 2011 - 10:31 AM

Sorry I meant CalcTotalCharges()
Was This Post Helpful? 0
  • +
  • -

#6 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: Hospital Form Question Why Wont it Work?

Posted 14 November 2011 - 11:23 AM

Because your CalcTotalCharges() method need parameter to be passes according to the definition but you passes nothing when you try to call it.
So you have either to pass something or change the definition of the method to a no parameter.

Quote

But don't functions default to Public so they can be used by the entire class?

I am talking about variables, not methods. Your variables are declared locally, means they can only be accessed in a method where they were defined, so every method will have its own variables with their own values, this simply means values assigned in one variable wont affect others in a different method
Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: Hospital Form Question Why Wont it Work?

Posted 14 November 2011 - 11:36 AM

A Couple Tutorials:

Scope of Variables
Debugging Skills
Was This Post Helpful? 1
  • +
  • -

#8 The_Chin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 13-November 11

Re: Hospital Form Question Why Wont it Work?

Posted 14 November 2011 - 11:38 AM

Ok I modified my code to this
 Private Sub btnCalculateCharges_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculateCharges.Click
        'Declare variables

        Dim decTotal As Decimal
        Dim decstaycharges As Decimal
        Dim decmisccharges As Decimal


       'Call the procedures to be added together
        decTotal = CalcTotalCharges(decstaycharges, decmisccharges)

        'Display the Result
        lblTotalCostDisplay.Text = decTotal.ToString

    End Sub

    Function CalcStayCharges() As Integer
        'Declare the price rate per night

        Dim decNightly_Rate As Decimal = 350D
        Dim intNumberofDays = 0
        Dim decstaycharges As Decimal

        decstaycharges = intNumberofDays * decNightly_Rate
        'Returns the amount for the number of nights stayed

        Return decstaycharges


    End Function

    Function CalcMiscCharges(ByVal decmedicationcharges As Decimal, ByVal decsurgicalcharges As Decimal, _
                              ByVal decphysicalrehabilitation As Decimal, ByVal declabfees As Decimal) As Decimal
        'this function calculates the misc charges
        Dim decMiscCharges As Decimal

        decMiscCharges = declabfees + decmedicationcharges + decphysicalrehabilitation + decsurgicalcharges

        Return decMiscCharges

    End Function

    Function CalcTotalCharges(ByRef decstaycharges As Decimal, ByRef decmisccharges As Decimal) As Decimal
        'this function calculates the total cost.
        Dim dectotal As Decimal

        dectotal = decstaycharges.ToString + decmisccharges.ToString

        Return dectotal
    End Function

    Private Sub btnClaearForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClaearForm.Click
        txtLabFees.Text = Nothing
        txtLegnthofStay.Text = Nothing
        txtMedicationCharges.Text = Nothing
        txtPhysicalRehabilitation.Text = Nothing
        txtSurgicalCharges.Text = Nothing
    End Sub
End Class



The total price still comes up as zero.
Was This Post Helpful? 0
  • +
  • -

#9 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: Hospital Form Question Why Wont it Work?

Posted 14 November 2011 - 11:51 AM

reread my tutorial on Scope of Variables
Was This Post Helpful? 0
  • +
  • -

#10 Tryparse  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 80
  • View blog
  • Posts: 193
  • Joined: 20-April 10

Re: Hospital Form Question Why Wont it Work?

Posted 14 November 2011 - 11:52 AM

Also, while this is far from the root of your problem, it is a problem. Let's look at your CalcTotalCharges function:
 Function CalcTotalCharges(ByRef decstaycharges As Decimal, ByRef decmisccharges As Decimal) As Decimal
    'this function calculates the total cost.
      
    CalcTotalCharges = decStayCharges.ToString + decMiscCharges.ToString

    Return CalcTotalCharges
End Function

There's a couple things wrong with this function.

First, why are you passing the parameters by reference? You're not changing the value of the parameters inside the function. You should only pass by reference when you absolutely have to, otherwise pass by value. If you pass by reference when you don't need to, you run the risk of changing the value of a parameter inside the function, which could then potentially corrupt data later in the program.

Second, if you're trying to add these charges together, why are you converting them to strings? At that point, you're not adding, you're concatenating. If you fed this function 55.50 and 100.25, you'd end up with 55.50100.25. Drop the .ToString if you're trying to add these values.

This post has been edited by Tryparse: 14 November 2011 - 11:56 AM

Was This Post Helpful? 1
  • +
  • -

#11 The_Chin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 13-November 11

Re: Hospital Form Question Why Wont it Work?

Posted 14 November 2011 - 12:13 PM

Thank you both, I modified my code to this
Public Class frmMain

    'Joseph Squillacioti
    'Id'007851935

    ' Purpose of application: To let the user input hospital charges onto a form, and then calculate that information
    'Declare variables

    Public decTotal As Decimal
    Public decstaycharges As Decimal
    Public decmisccharges As Decimal

    'Declare the price rate per night

    Public decNightly_Rate As Decimal = 350D
    Public intNumberofDays = 0


    Private Sub btnCalculateCharges_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculateCharges.Click

        'Call the procedures to be added together
        decTotal = CalcTotalCharges(decstaycharges, decmisccharges)

        'Display the Result
        lblTotalCostDisplay.Text = decTotal.ToString

    End Sub

    Function CalcStayCharges() As Integer


        decstaycharges = intNumberofDays * decNightly_Rate
        'Returns the amount for the number of nights stayed

        Return decstaycharges


    End Function

    Function CalcMiscCharges(ByVal decmedicationcharges As Decimal, ByVal decsurgicalcharges As Decimal, _
                              ByVal decphysicalrehabilitation As Decimal, ByVal declabfees As Decimal) As Decimal
        'this function calculates the misc charges

        decMiscCharges = declabfees + decmedicationcharges + decphysicalrehabilitation + decsurgicalcharges

        Return decMiscCharges

    End Function

    Function CalcTotalCharges(ByVal decstaycharges As Decimal, ByVal decmisccharges As Decimal) As Decimal
        'this function calculates the total cost.
        
        Return decstaycharges + decmisccharges
    End Function

    Private Sub btnClaearForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClaearForm.Click
        txtLabFees.Text = Nothing
        txtLegnthofStay.Text = Nothing
        txtMedicationCharges.Text = Nothing
        txtPhysicalRehabilitation.Text = Nothing
        txtSurgicalCharges.Text = Nothing
    End Sub
End Class



I get no error, it just still returns zero
Was This Post Helpful? 0
  • +
  • -

#12 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: Hospital Form Question Why Wont it Work?

Posted 14 November 2011 - 12:22 PM

Now do use the debugging skills and place a breakpoint on that function.
Was This Post Helpful? 0
  • +
  • -

#13 Tryparse  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 80
  • View blog
  • Posts: 193
  • Joined: 20-April 10

Re: Hospital Form Question Why Wont it Work?

Posted 14 November 2011 - 01:36 PM

You're not getting values for decmisccharges or decstaycharges anywhere. You should probably get values for those from textboxes or whatever in the btnCalculateCharges_Click sub before you call CalcTotalCharges.
Was This Post Helpful? 1
  • +
  • -

#14 The_Chin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 13-November 11

Re: Hospital Form Question Why Wont it Work?

Posted 14 November 2011 - 08:29 PM

Thank You, my application now works!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1