• 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.
  • Posts

    • Google's NotebookLM gets some useful features you may have been waiting for by Aditya Tiwari It's been three years since Google introduced its AI-powered note-taking and research app, NotebookLM. Just when Apple is about to kick off WWDC 2026, the search giant has announced a platter of new NotebookLM features that add agentic capabilities in chat and more advanced reasoning. For starters, NotebookLM now draws its fuel from Gemini 3.5 and Antigravity to improve accuracy and reliability. One of the things people have been asking for a long time is more transparency into the thinking steps. NotebookLM now shows expanded thinking steps in chat, providing better visibility into the thinking process. Google compared the upgraded NotebookLM with prior versions and found that it "achieved an average win rate of over 65% — a 15% point margin above parity — across our top five core evaluation dimensions," including accuracy & quality, multilingual support, large document analysis, document creation, and advanced research. It showed substantial improvements in analyzing large documents, achieving a 69.9% win rate. The system also delivered "exceptional performance" in advanced web research and source discovery with a 78.2% win rate. The AI research tool now generates outputs in more formats. You can give instructions to guide the outputs and download the generated files from the studio panel. Here are the newly supported formats: PNG and SVG for data visualizations and charts PDFs, docx, markdown, and text files for documents PNG, JPG, and GIF for images JSON and CSV for structured data XLSX for Microsoft Excel PPTX for Microsoft PowerPoint You can make edits after the outputs are generated. The feature is available globally; therefore, you can provide directions in one language and create outputs in another. Google said that it's also making it easier to get started with a project in NotebookLM. Instead of having a list of sources beforehand, you can even start with loose ideas, and NotebookLM can help build the repository of sources through the chat. For instance, you can find primary sources in other languages to get new perspectives or explore related works of an author. All of these new features are rolling out globally for those who can loosen up their pockets. They are available to users with Google AI Ultra and all Workspace business customers with AI Ultra access. Google has plans to expand them to more users in the future.
    • "...will no longer be bundled with the monthly Windows security updates or Patch Tuesdays. Instead the company is shifting delivery of these updates to Microsoft Update...". Three names that all refer to Windows Update. Does the author even understand what he is writing about?
    • and somehow windows is the only one with issue every patch tuesday is a mess, android macosx have all that and it never had issues locking people out of their own data.
    • It's the ol': I told my kids I was older than Google and they thought I was joking. For me I was born the same year as the internet
  • Recent Achievements

    • One Month Later
      amusc earned a badge
      One Month Later
    • One Month Later
      DJC50PLUS earned a badge
      One Month Later
    • Week One Done
      DJC50PLUS earned a badge
      Week One Done
    • Proficient
      Eric Biran went up a rank
      Proficient
    • Dedicated
      Conjor earned a badge
      Dedicated
  • Popular Contributors

    1. 1
      +primortal
      501
    2. 2
      PsYcHoKiLLa
      238
    3. 3
      Steven P.
      75
    4. 4
      ATLien_0
      75
    5. 5
      +Edouard
      69
  • Tell a friend

    Love Neowin? Tell a friend!