• 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

    • I think it depends on what you're looking for to do, and the time you have to spare. With my Dwarf 3, I easily spend 3-4 hour sessions; half an hour driving to an un-light polluted place, another half hour unpacking and setting up the smart scope + tripod for equatorial tracking, then 15 more minutes mucking around with settings and shooting calibration frames, spending a few hours shooting, merging with past photo sessions, etc. It's crazy how time flies and I often get home later than I expected. It's something I still need to set aside a good part of an evening to do, all in all. For one session, where you often need like four for best results when it comes to deep space objects. Even with a smart scope like Dwarf 3, regular non-astro photography is still way more approachable to people getting into photography. I find this is a time consuming niche no matter how I go about it. With practice, I can probably begin cutting time here but I think where smart scopes find their home is among people who love to shoot the night sky but don't have the spare time to go deep with the "navigator level" attunement to the night sky itself in addition to everything else. Having said this, _if_ you have even more time to spend on this hobby, it will probably be even more rewarding to do it more by hand and learn the skies and the details of how it all works.
    • I misread the title and thought Teams itself would be redesigned. Imagine having this one as a native WinUI app.
    • Dell, HP PCs ran into endless reboot, BitLocker recovery loops but Windows 11 isn't to blame by Sayan Sen Last month Neowin reported on a major issue on Dell systems wherein a bug in its official support tool was leading to endless blue screen of death (BSOD) and restarts. Following our report, Dell officially acknowledged its SupportAssist-related crash issue, confirming that the culprit is not Microsoft's operating system but rather a faulty version of its own remediation software. In a newly published support advisory, Dell stated that version 5.5.16.0 of Dell SupportAssist Remediation and Alienware SupportAssist Remediation can trigger blue screen errors and unexpected system restarts. The company notes that the problematic component operates independently of the main SupportAssist application, meaning users should not remove the primary SupportAssist software when troubleshooting the issue. According to Dell, the crashes are linked specifically to the SupportAssist Remediation service, which is bundled with SupportAssist OS Recovery Tools, and as such it has since released an updated version, 5.5.16.1, which is said to resolve the problem. Affected users are advised to first verify whether version 5.5.16.0 is installed by checking the Installed Apps section in Windows Settings. If so, Dell recommends updating SupportAssist OS Recovery Tools through either SupportAssist's "Update Software" feature or Dell Command Update. Dell also advises users to back up important data before performing the update and to ensure systems remain connected to power throughout the installation process. If you are still having issues though make sure to report to the Dell support forum. As it turns out though Dell is not the only PC maker currently dealing with update-related headaches as HP is also facing a separate but probably equally frustrating issue involving recent Windows Secure Boot updates that were released with recent Windows 11 Patch Tuesdays. Similar to Dell, HP also put up its own support article where it explains the issue. The company says that affected devices could hit a brick wall when booting as they run into a BitLocker recovery loop after the April 2026 updates. The problem appears to affect systems wherein the new UEFI Secure Boot CA 2023 certificates fail to apply properly. As such affected users will find themselves entering their recovery key over and over again despite the system otherwise functioning normally. HP says such PCs should be updated to the latest available BIOS version and configured with the necessary Secure Boot certificates before installing Microsoft's Windows 11 Patch Tuesday updates. Systems that are already experiencing the problem may require BIOS configuration changes to restore normal boot behavior. Admins can find information regarding that in the support article here on HP's official website.
    • Getting further away from the artistic study of mental disease that was the first game... (which never needed any sequels to begin with) But I get it, a company has to make money. And the second was at least visually impressive, if not in any other way.
    • If its the devs fault you would think Unreal would help M$ take full advantage of Unreal and work with them to fix the performance issues. Otherwise they are catching unwarranted bad press.
  • Recent Achievements

    • 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
    • Week One Done
      Windows Guy earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      493
    2. 2
      PsYcHoKiLLa
      246
    3. 3
      Steven P.
      72
    4. 4
      +Edouard
      69
    5. 5
      neufuse
      68
  • Tell a friend

    Love Neowin? Tell a friend!