• 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
  On 18/11/2015 at 16:30, XerXis said:

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
  On 18/11/2015 at 16:56, alyssajohnson said:

"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
  On 18/11/2015 at 17:13, alyssajohnson said:

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
  On 18/11/2015 at 17:08, _Alexander said:

       private decimal TotalCharges()
        {
        decimal total;

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

        }

  On 18/11/2015 at 19:05, Andre S. said:

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
  On 18/11/2015 at 17:13, alyssajohnson said:

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

    • Zen Browser 1.13b by Razvan Serea Zen Browser is a privacy-focused, open-source web browser built on Mozilla Firefox, offering users a secure and customizable browsing experience. It emphasizes privacy by blocking trackers, ads, and ensuring your data isn't collected. With Zen Mods, users can enhance their browser experience with various customization options, including features like split views and vertical tabs. The browser is designed for efficiency, providing fast browsing speeds and a lightweight interface. Zen Browser prioritizes user control over the browsing experience, offering a minimal yet powerful alternative to traditional web browsers while keeping your online activity private. Zen Browser’s DRM limitation Zen Browser currently lacks support for DRM-protected content, meaning streaming services like Netflix and HBO Max are inaccessible. This is due to the absence of a Widevine license, which requires significant costs and is financially unfeasible for the developer. Additionally, applying for this license would require Zen to be part of a larger company, similar to Mozilla or Brave. Therefore, DRM-protected media won't be supported in Zen Browser for the foreseeable future. Zen Browser offers features that improve user experience, privacy, and customization: Privacy-Focused: Blocks trackers and minimizes data collection. Automatic Updates: Keeps the browser updated with security patches. Zen Mods: Customizable themes and layouts. Workspaces: Organize tabs into different workspaces. Compact Mode: Maximizes screen space by minimizing UI elements. Zen Glance: Quick website previews. Split Views: View multiple tabs in the same window. Sidebar: Access bookmarks and tools quickly. Vertical Tabs: Manage tabs vertically. Container Tabs: Separate browsing sessions. Fast Profile Switcher: Switch between profiles easily. Tab Folders: Organize tabs into folders. Customizable UI: Personalize browser interface. Security Features: Inherits Firefox’s robust security. Fast Performance: Lightweight and optimized for speed. Zen Mods Customization: Deep customization with mods. Quick Access: Easy access to favorite websites. Open Source: Built on Mozilla Firefox with community collaboration. Community-Driven: Active development and feedback from users. GitHub Repository: Contribute and review the source code. Zen Browser 1.13b changes: New Features There's a new way to manage spaces, which brings a more intuitive and user-friendly experience Updated to firefox 139.0.4 Added support for Google safebrowsing for better security Collapsed toolbarr gets a slight UI redesign Fixes Fixed issues related to glance and split view Fixed performance issues and high GPU usage for some users Other small fixes and improvements Breaking Changes Customizable UI buttons at the bottom has been reset to a new default state Download: Zen Browser | 73.6 MB (Open Source) Download: Zen Browser ARM64 | Other Operating Systems View: Zen Browser Home Page | Screenshots 1 | 2 | Reddit Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • TBF, it has had PST support for quite a while now. But I still want them to add the ability to drag & drop between accounts / PSTs.
    • LibreOffice closes in on Microsoft Office, leaves Windows 7/8 behind in 25.8 Beta 1 by David Uzondu The Document Foundation has released LibreOffice 25.8 Beta 1 for public testing on Linux, macOS, and Windows. This is the second pre-release for the 25.8 cycle and the foundation says that the final, stable version of LibreOffice 25.8 is expected to land at the end of August 2025. Starting off with Writer, LibreOffice's Word, the developers have finally addressed some long-standing annoyances, including a new command to easily insert a paragraph break right before a table. This beta also introduces a useful privacy feature in its Auto-Redact tool, letting you strip all images from a document with a single option. To use it, go to Tools and select the Auto-Redact option: The application has improved its ability to handle different languages for punctuation, preventing mix-ups in multilingual documents. Other notable improvements have also been made. A new hyphenation rule lets you choose to prevent a word from splitting at the end of a page, moving the whole line to the next page instead. Microsoft Word has had this feature for years now. The Navigator now displays a handy tooltip with word and character counts for headings and their sub-outlines. Scrolling behavior when selecting text has been improved, making it less erratic. A new command with a keyboard shortcut was added for converting fields into plain text. Calc gets a lot of new functions that bring it closer to its competitors like Excel, including TEXTSPLIT, VSTACK, and WRAPROWS. Impress now properly supports embedded fonts in PPTX files, which should reduce headaches when sharing presentations with PowerPoint users. Alongside these additions, the project is also cleaning house; support for Windows 7, 8, and 8.1 has been completely dropped. There are also smaller UI tweaks across the suite, like allowing a single click to enter rotation mode for objects in Writer and Calc. macOS users get better integration, with proper support for native full screen mode and new window management features from the Sequoia update. In terms of performance, the team has optimized everything from loading huge DOC files and XLSX spreadsheets with tons of conditional formatting to simply switching between sheets in Calc. These improvements should be noticeable, especially when working with complex documents. A new application-wide "Viewer mode" has also been implemented, which opens all files in a read-only state for quick, safe viewing. On a related note, The Document Foundation has joined efforts by the likes of KDE to encourage Windows 10 users to switch to Linux. Also, you might have heard that Denmark, in a bid to lessen its reliance on Microsoft, has decided to make a full switch to LibreOffice, with plans to begin phasing out Office 365 in certain ministries as early as next month. If you're interested in this release, you can read the full release notes and download the binaries for your platform: Windows, macOS (Intel | Apple Silicon), or Linux (DEB | RPM). You can also get the latest stable version from our software stories page.
  • Recent Achievements

    • Week One Done
      julien02 earned a badge
      Week One Done
    • One Year In
      Drewidian1 earned a badge
      One Year In
    • Explorer
      Case_f went up a rank
      Explorer
    • Conversation Starter
      Jamie Smith earned a badge
      Conversation Starter
    • First Post
      NeoToad777 earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      544
    2. 2
      ATLien_0
      227
    3. 3
      +FloatingFatMan
      160
    4. 4
      Michael Scrip
      113
    5. 5
      +Edouard
      104
  • Tell a friend

    Love Neowin? Tell a friend!