10 Replies - 4711 Views - Last Post: 21 November 2012 - 09:40 PM Rate Topic: -----

#1 high5withknives   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 21-November 12

Hospital charge form. Don't understand the errors.

Posted 21 November 2012 - 02:46 PM

 private void calcButton_Click(object sender, EventArgs e)
        {
            public double CalcStayCharges
            {
                double total;
                double dailyCharge = 350;
                totalBox = daysSpent * dailyCharge;
                return total;
            }
        public double CalcMiscCharges(double meds, double surgCharges, double labFees, double physRehab)
        {
            double miscCharges;
            miscCharges = meds + surgCharges + labFees + physRehab;
            return miscCharges;
        }
        public double CalcTotalCharges(miscCharges, total);
    {
        double totalCharges;
        double medCharges;
        double surgCharges;
        double labFees;
        double physRehab;
        double total;
        double miscCharges;

        return totalCharges = (miscCharges, total);

        totalLabel.Text = totalCharges.ToString("c");
    }
    
        double meds, surgCharges, labFees, physRehab, daysSpent;
    try
{
    medCharges = double.Parse(medChargeBox.Text);
    daysSpent = double.Parse(dayChargeBox.Text);
    labFees = double.Parse(labFeeBox.Text);
    surgCharges = double.Parse(surgicalChargeBox.Text);
    physRehab = double.Parse(rehabFeeBox.Text);
}




I don't understand why these are errors, I feel as though this is exactly as the book is teaching it. I'm not looking for my homework to be done for me as I would like to learn why what I am doing is wrong, just looking for direction.

Is This A Good Question/Topic? 0
  • +

Replies To: Hospital charge form. Don't understand the errors.

#2 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Hospital charge form. Don't understand the errors.

Posted 21 November 2012 - 03:11 PM

Do you think that informing us what the actual errors are, would be helpful?
Was This Post Helpful? 0
  • +
  • -

#3 high5withknives   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 21-November 12

Re: Hospital charge form. Don't understand the errors.

Posted 21 November 2012 - 03:35 PM

I am terribly sorry. I have reworked the entire code, now with no errors, it just doesn't output anything

 private void calcButton_Click(object sender, EventArgs e)
        {
            double medCharges;
            double surgCharges;
            double labFees;
            double physRehab;
            double daysSpent;
            try
            {
                medCharges = double.Parse(medChargeBox.Text);
                daysSpent = double.Parse(dayChargeBox.Text);
                labFees = double.Parse(labFeeBox.Text);
                surgCharges = double.Parse(surgicalChargeBox.Text);
                physRehab = double.Parse(rehabFeeBox.Text);
            }
            catch
            {
            }
        }
        public double CalcStayCharges(double daysSpent)
        {
            double total;
            double dailyCharge = 350;
            total = daysSpent * dailyCharge;
            return total;
        }
        public double CalcMiscCharges(double medCharges, double surgCharges, double labFees, double physRehab)
        {
            double miscCharges;
            miscCharges = medCharges + surgCharges + labFees + physRehab;
            return miscCharges;
        }
        public double CalcTotalCharges(double miscCharges, double total)
        {
            double totalCharges;
          totalCharges = miscCharges + total;
          totalLabel.Text = totalCharges.ToString("c");
            return totalCharges;
        }
    } 
}


Was This Post Helpful? 0
  • +
  • -

#4 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1020
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: Hospital charge form. Don't understand the errors.

Posted 21 November 2012 - 04:25 PM

Where do you output anything? You don't assign any values to controls that display things, so why would you expect it to magically display values?
Was This Post Helpful? 0
  • +
  • -

#5 high5withknives   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 21-November 12

Re: Hospital charge form. Don't understand the errors.

Posted 21 November 2012 - 04:28 PM

namespace _333_DamronM_Lab06
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public double CalcStayCharges(double daysSpent)
        {
            double total;
            double dailyCharge = 350;
            total = daysSpent * dailyCharge;
            return total;
        }
        public double CalcMiscCharges(double medCharges, double surgCharges, double labFees, double physRehab)
        {
            double miscCharges;
            miscCharges = medCharges + surgCharges + labFees + physRehab;
            return miscCharges;
        }
        public double CalcTotalCharges(double miscCharges, double total)
        {
            double totalCharges;
          totalCharges = miscCharges + total;
          totalLabel.Text = totalCharges.ToString("c");
            return totalCharges;
        }
        private void calcButton_Click(object sender, EventArgs e)
        {

            double medCharges;
            double surgCharges;
            double labFees;
            double physRehab;
            double daysSpent;
            
            if (double.TryParse(medChargeBox.Text, out medCharges))
                if (double.TryParse(dayChargeBox.Text, out daysSpent))
                    if (double.TryParse(labFeeBox.Text, out labFees))
                        if (double.TryParse(surgicalChargeBox.Text, out surgCharges))
                            if (double.TryParse(rehabFeeBox.Text, out physRehab))
                                if (medCharges >= 0)
                                    if (daysSpent >= 0)
                                        if (labFees >= 0)
                                            if (surgCharges >= 0)
                                                if (physRehab >= 0)
                                                    
                                                    totalLabel.Text = totalCharges.ToString("c");
            else
            {
                MessageBox.Show("Error");
            }
            else
            {
                MessageBox.Show("Error");
            }
            else
            {
                MessageBox.Show("Error");
            }
            else
            {
                MessageBox.Show("Error");
            }
            else
            {
                MessageBox.Show("Error");
            }
            else
            {
                MessageBox.Show("Error");
            }
            else
            {
                MessageBox.Show("Error");
            }
            else
            {
                MessageBox.Show("Error");
            }
            else
            {
                MessageBox.Show("Error");
            }
            else
            {
                MessageBox.Show("Error");
            }
        }   
        }
        
    } 




I feel like this has everything correct, but I don't quite understand what I need to do to get it to output the information.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6532
  • View blog
  • Posts: 14,447
  • Joined: 02-June 10

Re: Hospital charge form. Don't understand the errors.

Posted 21 November 2012 - 04:58 PM

First - Don't use the text in a textbox as a variable. Its horrible form.
Make a property that you can use throughout your application: Update that property when the text in the textbox changes. {see the properties tutorial linked in my signature block}

Second - Any time you see these huge blocks of if...else just STOP. You must know there has to be a better way than this if you just stop and plan a little more.
Attached Image


Assuming everything goes fine your totalLable.Text gets set. To what? totalCharges..... Uh... Where are you setting this? You double check that all your values are entered then do not call a method to actually perform the calculations and set totalCharges.

rookie: What this shows us is that you aren't familiar with breakpoints and how to debug your own code.


tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated NOV 2012
Spoiler



Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6532
  • View blog
  • Posts: 14,447
  • Joined: 02-June 10

Re: Hospital charge form. Don't understand the errors.

Posted 21 November 2012 - 05:09 PM

Some of my common tips (some may apply more than others to your specific style):
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like index, timeOut, row, column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
    You might want to look at some of the naming guidelines. Its a lot easier to start with good habits than to break bad habits later and re-learn.



  • Try to avoid having work actually take place in GUI control event handlers. It is better to have the GUI handler call other methods so those methods can be reused and make the code more readable. This is also how you can send parameters rather than use excessive global variables. Get in this habit even if you are using WinForms because WPF works a lot under the idea of "commands" and this will get you working towards that. Think of each gester, control click, menu option etc. as a command to do something such as a command to SAVE. It doesn't matter where the command comes from, all sources should point at the same target to do the actual saving.
    Spoiler


  • Don't replace lines of code that don't work. Instead comment them out and put your new attempts below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

    Spoiler

    If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.

Was This Post Helpful? 0
  • +
  • -

#8 high5withknives   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 21-November 12

Re: Hospital charge form. Don't understand the errors.

Posted 21 November 2012 - 06:32 PM

This is the way my professor has told me to do it. I just can't figure out to get it displayed. I don't know what the code is to fix it. I would seriously pay someone at this point to just explain in detail what I need to do to solve this, as if I were 5 years old.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace _333_DamronM_Lab06
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
        private void calcButton_Click(object sender, EventArgs e)
        {
            
            


            double medCharges;
            double surgCharges;
            double labFees;
            double physRehab;
            double daysSpent;

            if (double.TryParse(medChargeBox.Text, out medCharges))
                if (double.TryParse(dayChargeBox.Text, out daysSpent))
                    if (double.TryParse(labFeeBox.Text, out labFees))
                        if (double.TryParse(surgicalChargeBox.Text, out surgCharges))
                            if (double.TryParse(rehabFeeBox.Text, out physRehab))
                                if (medCharges >= 0)
                                    if (daysSpent >= 0)
                                        if (labFees >= 0)
                                            if (surgCharges >= 0)
                                                if (physRehab >= 0)
                                                    
                                                    totalLabel.Text = total.ToString("c");
                                                    
        
            else
            {
                MessageBox.Show("Please Enter a Value for Medical Charges");
                                        medChargeBox.Text = ("");
                                        medChargeBox.Focus();
            }
            else
            {
                MessageBox.Show("Please Enter a Value for Days");
                                        dayChargeBox.Text = ("");
                                        dayChargeBox.Focus();
            }
            else
            {
                MessageBox.Show("Please Enter a Value for Lab Fees");
                                        labFeeBox.Text = ("");
                                        labFeeBox.Focus();
            }
            else
            {
                MessageBox.Show("Please Enter a Value for Surgical Charges");
                                        surgicalChargeBox.Text = ("");
                                        surgicalChargeBox.Focus();
            }
            else
            {
                MessageBox.Show("Please Enter a Value for Physical Rehab Charges");
                                         rehabFeeBox.Text = ("");
                                         rehabFeeBox.Focus();
            }
            else
            {
                MessageBox.Show("Please Enter a Value of At Least 0 for Medical Charges");
                                         medChargeBox.Text =("");
                                         medChargeBox.Focus();
            }
            else
            {
                MessageBox.Show("Please Enter a Value of At Least 0 for Days");
                                         dayChargeBox.Text = ("");
                                         dayChargeBox.Focus();
            }
            else
            {
                MessageBox.Show("Please Enter a Value of At Least 0 for Lab Fees");
                                         labFeeBox.Text = ("");
                                         labFeeBox.Focus();
            }
            else
            {
                MessageBox.Show("Please Enter a Value of At Least 0 for Surgical Charges");
                                        surgicalChargeBox.Text = ("");
                                        surgicalChargeBox.Focus();
            }
            else
            {
                MessageBox.Show("Please Enter a Value of At Least 0 for Physical Rehab Charges");
                                        rehabFeeBox.Text = ("");
                                        rehabFeeBox.Focus();
            }
        }
        public double CalcStayCharges(double daysSpent)
        {
            double total;
            double dailyCharge = 350;
            total = daysSpent * dailyCharge;
            return total;
        }
        public double CalcMiscCharges(double medCharges, double surgCharges, double labFees, double physRehab)
        {
            double miscCharges;
            miscCharges = medCharges + surgCharges + labFees + physRehab;
            return miscCharges;
        }
        public double CalcTotalCharges(double miscCharges, double total)
        {
            double totalCharges;
            totalCharges = miscCharges + total;
            totalLabel.Text = totalCharges.ToString("c");
            return totalCharges;
        }

        
        private void ClearButton_Click(object sender, EventArgs e)
        {
            dayChargeBox.Text = ("");
            medChargeBox.Text = ("");
            surgicalChargeBox.Text = ("");
            labFeeBox.Text = ("");
            rehabFeeBox.Text = ("");
            dayChargeBox.Focus();


        }

    }   
    } 








I can't complete the if statement, if I haven't been clear enough.
Was This Post Helpful? 0
  • +
  • -

#9 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Hospital charge form. Don't understand the errors.

Posted 21 November 2012 - 07:33 PM

Then it is my opinion that your professor is an imbecile.




Focus stealing consider bad in my eyes.
Message box to convey error messages, they are better approaches.

namespace WindowsFormsApplication1
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void ValidateTextboxHasValueGreaterOrEqualToZero(object sender, CancelEventArgs e)
    {
      TextBox FromWhichTextbox = (TextBox)sender;
      double value=0;
      e.Cancel = !(Double.TryParse(FromWhichTextbox.Text, out value) && value >= 0);
      if (e.Cancel)
      {
        this.errorProvider1.SetError(FromWhichTextbox, "Numeric Value Required. (With a value greater or equal to zero.)");
      }
      else
      {
        this.errorProvider1.Clear();
      }
    }

  }
}



I'm using the ErrorProvider control rather the Message boxes, so that the control which contains the invalid stuff get a warning triangle beside it. By using the validation event I can restrict progressing to the next text box, until the contents are valid.


Designer Code
Spoiler


I've extracted out a couple of lines of the designer to highlight that you (I) can "wire up" the events of different controls to the same handler. ValidateTextboxHasValueGreaterOrEqualToZero

      this.MediChargeTB.Validating += new System.ComponentModel.CancelEventHandler(this.ValidateTextboxHasValueGreaterOrEqualToZero);
/* ... */
      this.DayChargeBox.Validating += new System.ComponentModel.CancelEventHandler(this.ValidateTextboxHasValueGreaterOrEqualToZero);






Then again a better choice of control at design maybe better suited to the job. Say like NumericUpDown.

The wire up the various ValueChanged Event to the same handler, which the calls a method to recalculate all of the price totals.(No need click a button!)
recalculateTotals()
{
 StayCharges = CalcStayCharges( this.DaySpent.Value, _dailyCharge);
 
/* etc */
}

const double _dailyCharge = 350;

public double CalcStayCharges(double daysSpent,double  dailyCharge)
{
          return daysSpent * dailyCharge;
       }



This post has been edited by AdamSpeight2008: 21 November 2012 - 07:41 PM

Was This Post Helpful? 2
  • +
  • -

#10 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Hospital charge form. Don't understand the errors.

Posted 21 November 2012 - 08:14 PM

NumericUpDown Control does't restrict the text to met the rest of the controls requirements. Eg DecimalPlaces to 2 and you can enter more and multiple decimal points. But the value changed event is fired.
It is if ValueChanged is being incorrect triggered internally. I think it should only fire if the text value is valid (as per the restrictions) and the numeric value has actually changed.
Was This Post Helpful? 1
  • +
  • -

#11 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6532
  • View blog
  • Posts: 14,447
  • Joined: 02-June 10

Re: Hospital charge form. Don't understand the errors.

Posted 21 November 2012 - 09:40 PM

View PostAdamSpeight2008, on 21 November 2012 - 08:33 PM, said:

Then it is my opinion that your professor is an imbecile.


I second that. I've said it before and I'll say it again: Go to class for the certificate. But work through some "Learn C# in 30 days" type books to actually learn something: And a lot faster.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1