• 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

    • they will use your data anyways, this way you at least get to take advantage of it, might as well use it
    • I think its a great thing, whoever messes with my msgs is going to be real bored. Even those who dont trust meta, like myself, if the gains translate into an improved communication, easiness, everyone will use it. The alternative is not use Meta Whatsapp since they will AI your msgs an resume it anyways
    • Write-Host “Initiate porno mode!” -ForegroundColor Red Read-Host “Press the any key to continue” Save as “PornoMode.ps1
    • The so called tri-fold phone is not a phone just like today's so called smartphones are not phones. They are computing camera devices with the ability to make and receive phone calls. For many the phone app is among the least used apps on these so called smartphones.
    • Backpack Hero and Figment are free to claim on the Epic Games Store by Pulasthi Ariyasinghe The Epic Games Store has just refreshed its weekly giveaways promotion, and there are two games up for grabs this time. Replacing the Sable freebie from last week, Backpack Hero and Figment have now landed for all PC gamers to claim. As always, you have seven days to add the latest indie game permanently to your Epic Games Store library. From the double giveaway, Backpack Hero comes in from indie developer Jaspel, offering a unique inventory management roguelike experience. Aside from collecting items to maximize the potential of a run, like with other roguelikes, here, your placement of the item in the backpack also matters. Each run offers randomly generated dungeons, enemies, and loot to grab. Next, Figment lands from Bedtime Digital Games. This isometric perspective action adventure game takes place in a surreal landscape with hand-drawn artwork, and everything is taking place inside a mind. You take the role of Dusty, the former voice of courage in the mind. The musical adventure involves trying to return Dusty to his old self while also helping the mind beat back its nightmares and restoring its original purpose. The Figment and Backpack Hero giveaways on the Epic Games Store are now active, and they will last until July 10. When not on sale, both games come in at $19.99 to purchase, but PC gamers can add the duo to their library for no cost during the seven-day timeframe. When the giveaways refresh next week, Epic Games plans on giving away two more games: the next musical adventure, Figment 2: Creed Valley, as well as the classic arcade-inspired shoot’em up Sky Racket.
  • Recent Achievements

    • Week One Done
      Devesh Beri earned a badge
      Week One Done
    • Week One Done
      956400 earned a badge
      Week One Done
    • First Post
      loose_observer earned a badge
      First Post
    • Week One Done
      BeeJay_Balu earned a badge
      Week One Done
    • Week One Done
      filminutz earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      454
    2. 2
      ATLien_0
      158
    3. 3
      +FloatingFatMan
      152
    4. 4
      Nick H.
      65
    5. 5
      +thexfile
      62
  • Tell a friend

    Love Neowin? Tell a friend!