• 0

Help with my C# project


Question

Hello everyone,

I'm currently enrolled in a college C# class. Now, for some reason I thought it'd be a good idea to take this course without having any prerequisites like intro to programming. To say nonetheless, I'm very afraid, lost, and confused. I've struggled through the semester, and tried to put a lot of effort into understanding the content. But, on this particular project of "Joe's Automotive" I'm lost on what's wrong with the code. I know exactly what the issue is,(  every instance I have serviceTextBox.Text = total.ToString("c");  it's giving me an error) I'm just not entirely sure how to fix it. (For the record, I did ask for teacher assistance and she seemed lost herself on VS was mad about it.)

I'd really appreciate it if I could get some help on this, explaining what I could have done wrong already, and explaining how to fix it

Thank you for your time,

Alyssa

Here's the code:

//Alyssa Johnson
//Chapter 6 Project
//Joe's Automotive

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

namespace Chapter_6_PROJECT
{
    public partial class chapter6 : Form
    {
        public chapter6()
        {
            InitializeComponent();
        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void exitButton_Click(object sender, EventArgs e)
        {

            //closes out the program
            this.Close();
        }
        //calculates all of the values
        private void calculateButton_Click(object sender, EventArgs e)
        {

            //variables for later on in the code
            OilLubeCharges();
            Flushes();
            Misc();
            OtherCharges();
            Tax();
            TotalCharges();
            
            
        }

        //method for the oil and lube charges
        //this is for the Oil & Lube group Box
        private int OilLubeCharges()
        {
            int total = 0;

           

            //if they checked oil add 26 for their total
            if (oilCheckBox.Checked == true)
            {
                total += 26;
                //serviceAndLaborLabel.Text = total.ToString("c");
                serviceTextBox.Text = total.ToString("c");
                //return total;

             
            }

            //In case lube was checked, to add 18 dollars
            if (lubeCheckBox.Checked)
            {
                total += 18;
                //adding 18 to the total becuase that's how much the lube job would cost
                //serviceAndLaborLabel.Text = total.ToString("c");

                return total;

            }
            else
            {
                return total;
            }
        }

        //to calculate the flushes
        //private void for all of the group boxes.
        private int Flushes()
        {
            //local variable
            int total = 0;

            //if they checked off radiator to add 30

            if (radiatorCheckBox.Checked == true)
            {
                total += 30;
                serviceTextBox.Text = total.ToString("c");
            }

            //if they checked transmission to add 80 dollars

            if (transmissionCheckBox.Checked == true) //this is saying if "they check this" than it means this:
            {
                //adds 80 because that's how much transmissions cost
                total += 80;
                serviceTextBox.Text = total.ToString("c");
                return total;

            }
            else
            {
                //returns the total value
                return total;
            }
        }
        //private voids for all of the group boxes
        private int Misc()
        {
            //local variables
            int total = 0;

            //if they checked off inspection to add 15 dollars
            if (inspectionCheckBox.Checked == true)
            {
                total += 15;
                serviceTextBox.Text = total.ToString("c");

            }
            
            
            //if they checked off muffler
            if (mufflerCheckBox.Checked == true)  //if they check off muffler it means this :
            {
                total += 100;
                serviceTextBox.Text = total.ToString("c");

            }


            //Checked off tire rotation

            if (tireCheckBox.Checked == true) //if they check off that they want the tire rotation it means this:
            {
                total += 20;
                serviceTextBox.Text = total.ToString("c");
                return total;

            }
            else
            {
                return total;
            }
        }


        //this is for the Parts and Labor Group Box
        private int OtherCharges()
        {
            int labor;
            int parts;
            int total = 0;


            if (int.TryParse(laborTextBox.Text, out labor))

            {
            serviceTextBox.Text = labor.ToString("c");
            total = labor;

            return total;
            }


            if (int.TryParse(partsTextBox.Text, out parts))
            {
              partsLabel.Text = parts.ToString("c");
              total = parts;
              return total;

            }
            else
            {
                return total;
             }

 

        }
        private decimal Tax()
        {
            //for the taxes on all of this

            decimal addTax;
            decimal tax = 0;
            decimal parts;
            decimal totalParts = 0;

            if (decimal.TryParse(partsTextBox.Text, out parts))
            {
                totalParts = parts;
                partsLabel.Text = totalParts.ToString("c");

                parts = decimal.Parse(partsTextBox.Text);
                tax = parts * 0.06m;
                taxLabel.Text = tax.ToString("c");
                return tax;

                if (decimal.TryParse(taxOnPartLabel.Text, out addTax))
                {
                    tax = totalParts * 0.06m;
                    taxOnPartLabel.Text = tax.ToString("c");
                    return tax;
                }
                else
                {
                    return totalParts;
                }

            }
            else
            {
                return tax;
            }
        

        }


        //for the total
        private decimal TotalCharges()
        {
        decimal total;

        total = OilLubeCharges() + Flushes() + Misc() + OtherCharges() + Tax() + TotalCharges();
        totalLabel.Text = total.ToString("c");
        return total;

        }
        //the button for clearing out everything in the output Labels,radioButtons, and ect.
        private void clearButton_Click(object sender, EventArgs e)
        {
            //to clear out everything on the project
            oilCheckBox.Checked = false;
            lubeCheckBox.Checked = false;
            radiatorCheckBox.Checked = false;
            transmissionCheckBox.Checked = false;
            transmissionCheckBox.Checked = false;
            inspectionCheckBox.Checked = false;
            mufflerCheckBox.Checked = false;
            tireCheckBox.Checked = false;
            partsTextBox.Text = "";
            laborTextBox.Text = "";
            serviceTextBox.Text = "";
            partsLabel.Text = "";
            taxLabel.Text = "";
            totalLabel.Text = "";

            
        }

        


                


    }
}

 

VS.JPG

Link to comment
https://www.neowin.net/forum/topic/1279060-help-with-my-c-project/
Share on other sites

12 answers to this question

Recommended Posts

  • 0

You will get a stack overflow (your program will blow up) if you call TotalCharges, because it calls itself recursively with no way to ever exit. I think you need to clean up the calculation process. I don't think the *.Text = *.ToString("c") is actually a problem.

  • 0

"An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll"

 

That's the exact error I'm getting. Also, I wasn't using integers earlier in the code because there isn't a reason to get a number with a decimal pre tax. Do you think i should still change everything to decimal?

  • 0

They are still teaching you winforms? Talk about backwards technology :)

Anyway, what is the exact error you are getting?

uh it's still a HIGHLY used design standard in a LOT of places... WPF never really caught on that much outside of a few places... and Windows 8+ style apps are still not used on older OS's...

  • 0

"An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll"

 

That's the exact error I'm getting. Also, I wasn't using integers earlier in the code because there isn't a reason to get a number with a decimal pre tax. Do you think i should still change everything to decimal?

       private decimal TotalCharges()
        {
        decimal total;

        total = OilLubeCharges() + Flushes() + Misc() + OtherCharges() + Tax() + TotalCharges();
        totalLabel.Text = total.ToString("c");
        return total;

        }

  • 0

It depends on the program requirements. Don't allocate memory for a type if you are not going to require that type. If your program requirements are that the prices are only going to be in whole dollars, int works fine. If there is a chance that the prices will include change, use decimal. Just be aware that when using int, if you are doing math with ints where the answer stores to a decimal you will have to cast the ints as decimals otherwise you will run into a type mismatch.

  • 0

Sorry if this sounds stupid, I'm still very new to C# and programming in general... but, I don't know what's particularly wrong with that block of code. Could someone explain to me why I can't do it that way?

 

It looks like TotalCharges() calls TotalCharges() calls TotalCharges() call TotalCharges() call TotalCharges() ... until StackOverflowException

  • 0

Tip: learn to set breakpoints and step through your code using the debugger ASAP. You'd be able to see exactly what's going on, step by step. If you step through your TotalCharges function you'll see that it calls itself indefinitely.

  • 0

       private decimal TotalCharges()
        {
        decimal total;

        total = OilLubeCharges() + Flushes() + Misc() + OtherCharges() + Tax() + TotalCharges();
        totalLabel.Text = total.ToString("c");
        return total;

        }

Tip: learn to set breakpoints and step through your code using the debugger ASAP. You'd be able to see exactly what's going on, step by step. If you step through your TotalCharges function you'll see that it calls itself indefinitely.

What these two said:

  1. You cant call the same Method within the Method, without a way out. (it doesn't make sense)
  2. Add break point and step through the code, you will see that it is call itself like n* times
  • 0

Sorry if this sounds stupid, I'm still very new to C# and programming in general... but, I don't know what's particularly wrong with that block of code. Could someone explain to me why I can't do it that way?

 

 

Looks like you wanted to use something else (or nothing at all) at the end of "total = OilLubeCharges() + Flushes() + Misc() + OtherCharges() + Tax() + TotalCharges();" but autocompleted to TotalCharges() at the end and you didn't notice because I see no reason to have it there.

This topic is now closed to further replies.