Results 1 to 17 of 17

Thread: Functions and validating

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2015
    Posts
    73

    Functions and validating

    I need some help with a homework assignment. I am supposed to create a few functions. one to calculate the charges for the number of days stayed at a hospital, another to calculate the charge for misc. charges, one to calculate the total charge, and a final one for validation. I'm not sure how close I am or how badly I have screwed the whole thing up, so if someone could help me out i would greatly appreciate it! oh and I do have to have option strict and option explicit on, if that matters.

    vb.net Code:
    1. Private DaysHospitalized As Short = CDec(txtDays.Text) 'Holds days hospitalized
    2.     Private Meds As Decimal = CDec(txtMeds.Text) 'Holds Medication charges
    3.     Private Surgical As Decimal = CDec(txtSurgical.Text) 'Holds Surgery charges
    4.     Private Lab As Decimal = CDec(txtLab.Text) 'Holds Lab testing charges
    5.     Private Physical As Decimal = CDec(txtPhysical.Text) 'Holds Physical Therapy charges
    6.     Const COST_PER_DAY As Decimal = 350D 'Constant that holds the cost per day.
    7.     'Function to calculate the charges based on number of days stayed.
    8.     Function CalcStayCharges(ByRef DaysHospitalized As Short, ByRef COST_PER_DAY As Decimal) As Decimal
    9.  
    10.         Dim StayCharges As Decimal = DaysHospitalized + COST_PER_DAY 'Holds the charges for days stayed.
    11.  
    12.         'Return Result.
    13.         Return StayCharges
    14.  
    15.     End Function
    16.     'Function to calculate all misc. charges
    17.     Function CalcMiscCharges(ByRef Meds As Decimal, ByRef Surgical As Decimal, ByRef Lab As Decimal, ByRef Physical As Decimal
    18.             ) As Decimal
    19.         Dim MiscCharges As Decimal = Meds + Surgical + Lab + Physical 'Holds the sum of misc. charges
    20.  
    21.         'Return Result.
    22.         Return MiscCharges
    23.  
    24.     End Function
    25.     'Function to calculate the total charges.
    26.     Function CalcTotalCharges(ByRef StayCharges As Decimal, ByRef MiscCharges As Decimal) As Decimal
    27.         Dim TotalCharges As Decimal = StayCharges + MiscCharges 'Holds the sum of StayCharges and MiscCharges
    28.  
    29.         'Return Result.
    30.         Return TotalCharges
    31.  
    32.     End Function
    33.  
    34.     '=================================================frmHospitalInfo=====================================================================
    35.     Private Sub frmHospitalInfo_Load(sender As Object, e As EventArgs) Handles Me.Load
    36.         frmSplash.ShowDialog() 'Display Splash form when loading program.
    37.     End Sub
    38.  
    39.     Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
    40.         Dim TotalCharges As Decimal
    41.  
    42.         TotalCharges = CalcTotalCharges()
    43.         lblTotalCost.Text = CalcTotalCharges()
    44.  
    45.  
    46.  
    47.  
    48.     End Sub
    49. End Class

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,446

    Re: Functions and validating

    Firstly, have you debugged your code, i.e. set a break point and stepped through the code?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2015
    Posts
    73

    Re: Functions and validating

    No, I didn't know I needed to. I will try that. I have never done it before.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2015
    Posts
    73

    Re: Functions and validating

    Ok so I put in a break point and tried to step through but my program is not updated based on the code. I originally did this program without creating any functions (because I read the directions poorely) so the whole thing worked perfectly, but now I deleted all that code and created my own functions, but I am getting errors. When I go to debug the program though it is working as if the old code is still in there. Is there a way to update it so I can debug correctly?

  5. #5
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,950

    Re: Functions and validating

    It sounds like you're running a build executable?, you run debug mode from the Designer. Set breakpoints then menu "Debug", "Start Debugging" or press "F5"


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2015 (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2015
    Posts
    73

    Re: Functions and validating

    I think you are right. I tried a few things and then it started reading the current code instead of what I had deleted. I had to go but I will try debugging it when I get home. Hopefully that will help me find the problem.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2015
    Posts
    73

    Re: Functions and validating

    It wont even let me Step into when trying to debug. I am getting two errors.
    1. Error 1 Argument not specified for parameter 'MiscCharges' of 'Public Function CalcTotalCharges(ByRef StayCharges As Decimal, ByRef MiscCharges As Decimal) As Decimal'. C:\Users\Ethan\Documents\Visual Studio 2013\Projects\waHospitalCharges\waHospitalCharges\frmMain.vb 58 24 waHospitalCharges

    and

    2. Error 1 Argument not specified for parameter 'MiscCharges' of 'Public Function CalcTotalCharges(ByRef StayCharges As Decimal, ByRef MiscCharges As Decimal) As Decimal'. C:\Users\Ethan\Documents\Visual Studio 2013\Projects\waHospitalCharges\waHospitalCharges\frmMain.vb 58 24 waHospitalCharges

    This seems to be related to where I am trying to set TotalCharges = CalcTotalCharges() but I don't understand enough about functions to understand what I am doing wrong. I have read through my text book, tried to copy what mine is compared to the books examples, and read on a few websites and through the help menu and can't figure this out. I just don't understand functions very well. If someone can explain it to me clearly it would be great.

    vb.Net Code:
    1. Private DaysHospitalized As Short = CShort(txtDays.Text)
    2.     Private Meds As Decimal = CDec(txtMeds.Text) 'Holds Medication charges
    3.     Private Surgical As Decimal = CDec(txtSurgical.Text) 'Holds Surgery charges
    4.     Private Lab As Decimal = CDec(txtLab.Text) 'Holds Lab testing charges
    5.     Private Physical As Decimal = CDec(txtPhysical.Text) 'Holds Physical Therapy charges
    6.     Private Const COST_PER_DAY As Decimal = 350D 'Constant that holds the cost per day.
    7.     'Function to calculate the charges based on number of days stayed.
    8.     Function CalcStayCharges(ByRef DaysHospitalized As Short, ByRef COST_PER_DAY As Decimal) As Decimal
    9.  
    10.         Dim StayCharges As Decimal = DaysHospitalized * COST_PER_DAY 'Holds the charges for days stayed.
    11.  
    12.         'Return Result.
    13.         Return StayCharges
    14.  
    15.     End Function
    16.     'Function to calculate all misc. charges
    17.     Function CalcMiscCharges(ByRef Meds As Decimal, ByRef Surgical As Decimal, ByRef Lab As Decimal, ByRef Physical As Decimal
    18.             ) As Decimal
    19.         Dim MiscCharges As Decimal = Meds + Surgical + Lab + Physical 'Holds the sum of misc. charges
    20.  
    21.         'Return Result.
    22.         Return MiscCharges
    23.  
    24.     End Function
    25.     'Function to calculate the total charges.
    26.     Function CalcTotalCharges(ByRef StayCharges As Decimal, ByRef MiscCharges As Decimal) As Decimal
    27.         Dim TotalCharges As Decimal = StayCharges + MiscCharges 'Holds the sum of StayCharges and MiscCharges
    28.  
    29.         'Return Result.
    30.         Return TotalCharges
    31.  
    32.     End Function
    33.     '===================================================btnCalculate======================================================================
    34.     Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
    35.         Dim TotalCharges As Decimal
    36.  
    37.         TotalCharges = CalcTotalCharges()
    38.         lblTotalCost.Text = TotalCharges.ToString
    39.  
    40.     End Sub

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,239

    Re: Functions and validating

    Those private variables declared at the beginning won't work. Those are form level variables, so the code you used (the CDec(whatever) statements) will execute before the constructor of the form runs. At that time, the controls on the form haven't been created, yet, so all those CDec calls will fail.

    Another point is that you shouldn't be passing those arguments ByRef. By default, they will be passed ByVal, which is what you want. ByRef has its purposes, and this isn't one of them.

    I applaud your desire to dive into coding, because I like that approach myself, but you would really benefit from some structured approach to this. This site is one that is often recommended here:

    http://www.homeandlearn.co.uk/NET/vbNet.html

    While it's a linnear tutorial, you can jump around.
    My usual boring signature: Nothing

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Mar 2015
    Posts
    73

    Re: Functions and validating

    So I got this to work. The program runs perfectly...except I can't make a Validate Function. I tried this:

    Function ValidateInputFields() As Boolean
    If Not Decimal.TryParse(txtDays, DaysHospitalized) And that is as far as I get because it won't accept DaysHospitalized. I'm not sure what it wants in there. I barely understand how to make a function and I don't know if I did them right, and I definitely don't know how to make a function with TryParse in it. can someone please help. Thank you!

    vb.net Code:
    1. 'Function to calculate the charges based on number of days stayed.
    2.     Function CalcStayCharges() As Decimal
    3.         Dim DaysHospitalized As Short = CShort(txtDays.Text) 'Holds the number of days stayed at the hospital.
    4.         Const COST_PER_DAY As Decimal = 350D 'Constant that holds the cost per day.
    5.         Dim StayCharges As Decimal 'Holds the total charge for days stayed
    6.  
    7.         StayCharges = CDec(DaysHospitalized * COST_PER_DAY) 'Calculates the charge for days stayed.
    8.  
    9.         'Return Result.
    10.         Return StayCharges
    11.  
    12.     End Function
    13.  
    14.     'Function to calculate all misc. charges
    15.     Function CalcMiscCharges() As Decimal
    16.         Dim Meds As Decimal = CDec(txtMeds.Text) 'Holds Medication charges
    17.         Dim Surgical As Decimal = CDec(txtSurgical.Text) 'Holds Surgery charges
    18.         Dim Lab As Decimal = CDec(txtLab.Text) 'Holds Lab testing charges
    19.         Dim Physical As Decimal = CDec(txtPhysical.Text) 'Holds Physical Therapy charges
    20.         Dim MiscCharges As Decimal
    21.  
    22.         MiscCharges = Meds + Surgical + Lab + Physical 'Holds the sum of misc. charges
    23.  
    24.         'Return Result.
    25.         Return MiscCharges
    26.  
    27.     End Function
    28.     'Function to calculate the total charges.
    29.     Function CalcTotalCharges(ByRef StayCharges As Decimal, ByRef MiscCharges As Decimal) As Decimal
    30.         Dim TotalCharges As Decimal = StayCharges + MiscCharges 'Holds the sum of StayCharges and MiscCharges
    31.  
    32.         'Return Result.
    33.         Return TotalCharges
    34.  
    35.     End Function
    36.     '===================================================btnCalculate======================================================================
    37.     Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
    38.      
    39.         Dim TotalCharges As Decimal = CalcStayCharges() + CalcMiscCharges()
    40.  
    41.         lblTotalCost.Text = TotalCharges.ToString
    42.  
    43.     End Sub

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,239

    Re: Functions and validating

    You declared DaysHospitalized as type Short, which is a short integer, yet you tried to parse it as a Decimal. That may not be the problem, though, because I don't see the function you mention, so the other problem could have to do with the scope of the variables. The only place I see DaysHospitalized is as a local variable in CalcStayCharges(), which would mean that the variable wasn't accessible anywhere else.

    There are a couple general issues. I like the fact that you are converting strings to numbers, and you are using the right types in most cases. There's no good reason to use a Short...EVER. It's really just there for some legacy work. It may seem efficient to use a Short, since the number of days hospitalized had better not be higher than the size of a Short, but on modern computers (anything since the days of DOS, really), the Integer is the most efficient type, and your Short will probably be treated as an Integer, so you might as well just use an Integer and never use a Short.

    You are correct to be using Decimals for the other things, though, as those look like money, and Decimal is the right type for that. However, CDec will crash if the text passed to it is either an empty string or has any character that isn't a number. Since you are talking about validating, and are correctly using Decimal.TryParse (except that you have to pass it a Decimal rather than a Short as the second argument), it seems like you've already figured that one out.
    My usual boring signature: Nothing

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Mar 2015
    Posts
    73

    Re: Functions and validating

    See I thought I needed my variables to be public to the whole class to make the validate function read them but when I put them as public under the class I get an error saying they are null??? And how can I make 1 validate function that will validate for all the inputs even if that one is a short (or integer)? Is there a way to make a function that can be used to validate all of the inputs? The one I was making will just work for one of the inputs I think, but I'm really supposed to have 1 function to validate all inputs...

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,239

    Re: Functions and validating

    If you want them available to the whole form, then they have to be declared outside of any method. They probably shouldn't be Public, because that would make them visible outside of the class itself, whereas you just say that you want them visible throughout the class, which sounds right to me. Therefore, you want to declare them Private outside of any method, as you had them in the snippet in post #7. What you did wrong in post #7 was that you didn't just declare them outside of the methods, you also set them to something. You can do that in some cases, but you can't set them to be a value from a control at that time, because the control doesn't even exist at that time, and even if the control did exist, it wouldn't have anything in it.

    So what you need to do is declare the variable:

    Private Meds As Decimal
    etc.

    but don't set them to anything at that time. In your validate method, you would just do something like this:
    Code:
    If Decimal.TryParse(txtMeds.Text, Meds) Then
     'It was a decimal, and Meds now holds it.
    Else
     'It wasn't a decimal, so scold the user.
    End If
    The Validate method can have as many of those If blocks as you need for the variables. They'd all look about the same, except that one would be an integer rather than a decimal.
    My usual boring signature: Nothing

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Mar 2015
    Posts
    73

    Re: Functions and validating

    Ok this is what I have now. The validate function is working but something is wrong somewhere in my calculation functions because I am only getting 0 back as an output for Total Charges. I have checked everything and don't see any issues so I'm not sure what is going on.

    vb.net Code:
    1. Public Class frmHospitalInfo
    2.  
    3.     Private DaysHospitalized As Integer 'Holds the number of days stayed at the hospital.
    4.     Private Const COST_PER_DAY As Decimal = 350D 'Constant that holds the cost per day.
    5.     Private StayCharges As Decimal 'Holds the total charge for days stayed
    6.     Private Meds As Decimal 'Holds Medication charges
    7.     Private Surgical As Decimal 'Holds Surgery charges
    8.     Private Lab As Decimal 'Holds Lab testing charges
    9.     Private Physical As Decimal 'Holds Physical Therapy charges
    10.     Private MiscCharges As Decimal 'Holds total Misc Charges
    11.     Private TotalCharges As Decimal 'Holds the sum of StayCharges and MiscCharges
    12.  
    13.     'Function to validate inputs.
    14.     Function ValidateInputFields() As Boolean
    15.         If CInt(txtDays.Text) < 0 Or txtDays.Text = String.Empty Then
    16.             MessageBox.Show("Please enter a positive numeric number for Days Stayed.")
    17.             txtDays.Clear()
    18.             Return False
    19.  
    20.         End If
    21.  
    22.         If CDec(txtMeds.Text) < 0 Or txtMeds.Text = String.Empty Then
    23.             MessageBox.Show("Please enter a positive numeric number for Medication cost.")
    24.             txtMeds.Clear()
    25.             Return False
    26.  
    27.         End If
    28.  
    29.         If CDec(txtSurgical.Text) < 0 Or txtSurgical.Text = String.Empty Then
    30.             MessageBox.Show("Please enter a positive numeric number for Surgery cost.")
    31.             txtSurgical.Clear()
    32.             Return False
    33.  
    34.         End If
    35.  
    36.         If CDec(txtLab.Text) < 0 Or txtLab.Text = String.Empty Then
    37.             MessageBox.Show("Please enter a positive numeric number for Lab cost.")
    38.             txtLab.Clear()
    39.             Return False
    40.  
    41.         End If
    42.  
    43.         If CDec(txtPhysical.Text) < 0 Or txtPhysical.Text = String.Empty Then
    44.             MessageBox.Show("Please enter a positive numeric number for Physical Therapy cost.")
    45.             txtPhysical.Clear()
    46.             Return False
    47.  
    48.         End If
    49.         Return True
    50.     End Function
    51.  
    52.     'Function to calculate the charges based on number of days stayed.
    53.     Function CalcStayCharges() As Decimal
    54.         DaysHospitalized = CInt(txtDays.Text) 'Sets DaysHospitalized to the corresponding text box.
    55.  
    56.         StayCharges = CDec(DaysHospitalized * COST_PER_DAY) 'Calculates the charge for days stayed.
    57.  
    58.         'Return Result.
    59.         Return StayCharges
    60.  
    61.     End Function
    62.  
    63.     'Function to calculate all misc. charges
    64.     Function CalcMiscCharges() As Decimal
    65.         Meds = CDec(txtMeds.Text) 'Sets Meds to the corresponding text box.
    66.         Surgical = CDec(txtSurgical.Text) 'Sets Surgical to the corresponding text box.
    67.         Lab = CDec(txtLab.Text) 'Sets Lab to the corresponding text box.
    68.         Physical = CDec(txtPhysical.Text) 'Sets Physical to the corresponding text box.
    69.  
    70.  
    71.         MiscCharges = Meds + Surgical + Lab + Physical 'Holds the sum of misc. charges
    72.  
    73.         'Return Result.
    74.         Return MiscCharges
    75.  
    76.     End Function
    77.     'Function to calculate the total charges.
    78.     Function CalcTotalCharges() As Decimal
    79.  
    80.         TotalCharges = StayCharges + MiscCharges 'Holds the sum of StayCharges and MiscCharges
    81.  
    82.         'Return Result.
    83.         Return TotalCharges
    84.  
    85.     End Function
    86.     '===================================================btnCalculate======================================================================
    87.     Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
    88.         If ValidateInputFields() Then
    89.  
    90.             lblTotalCost.Text = TotalCharges.ToString
    91.         End If
    92.     End Sub

  14. #14
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,239

    Re: Functions and validating

    You need to use Integer.TryParse and Decimal.TryParse. Using CInt or CDec will crash if the textbox is empty, regardless of the other part of the condition. The other part about TryParse is that it doesn't just check the number, it does the conversion, as well. In the example I showed, the first argument is the textbox, the second argument is the variable that will hold the value. So, if the conversion works, then Meds holds the correct value. The way you have it, you do the conversion with CDec or CInt, but you discard the result, so it never ends up in the variable in the validation method. You DO the conversion later on in CalcMiscCharges, but you never call it, so it never does anything. If you do the conversion in the Validate method, which you should, then there is no need to redo the conversion in CalcMiscCharges, but you do have to call CalcMiscCharges and CalcStayCharges at some time for them to be able to work. You'd also need to call CalcTotalCharges, so I would suggest that CalcTotalCharges should look like this:

    Code:
    Function CalcTotalCharges() As Decimal
     Return CalcMiscCharges() + CalcStayCharges()
    End Function
    My usual boring signature: Nothing

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Mar 2015
    Posts
    73

    Re: Functions and validating

    hmmm, I understand the issue with not calling functions. I fixed that issue and you were right it did fix the 0 I kept getting. I still don't understand why I need to use Decimal.TryParse though. The way I have it set up checks to make sure the input is not less then 0 or an empty string....which is what I need it to check. But the Decimal.TryParse and Integer.TryParse would just check to make sure that it can be converted to decimal or integer correct? Which still would leave the possibility of it being a negative number right? I really appreciate the help, the program is working as it should now, but I definitely would still like to really understand the TryParse issue, incase I need to use it later.

  16. #16
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,390

    Re: Functions and validating

    Have you gone away and read the documentation for tryparse? If the value is not a decimal your program will crash.

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Mar 2015
    Posts
    73

    Re: Functions and validating

    Yes I did but I didn't understand it exactly. I get it now, I played around with it and I get why it needs to be done like that. So I have that part covered and also I made it so it will not accept negative numbers. The last part of it I need it to be able to calculate even if a text box is left empty. I thought I could use an if statement that says for example If txtMeds.Text = String.Empty Then Meds = 0, that way the variable is initialized to 0 instead of just being empty. But that didn't work. How can I do this?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width