Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

James Mackey
PLUS
James Mackey
Courses Plus Student 994 Points

My program wont add my constants to the total. It only adds the textbox values i enter for labour and parts?

namespace WindowsFormsApplication10 { public partial class Form1 : Form { // Declare constant variables that do not change const decimal OIL_CHANGE = 26.00m; const decimal LUBE_JOB = 18.00m; const decimal RADIATOR_FLUSH = 30.00m; const decimal TRANSMISSION_FLUSH = 80.00m; const decimal INSPECTION = 15.00m; const decimal REPLACE_MUFFLER = 100.00m; const decimal TYRE_ROTATION = 20.00m; const decimal TAX = 0.23m; private decimal total = 0m; decimal serviceslabour; decimal parts; decimal tax; decimal fees;

    public Form1()
    {
        InitializeComponent();
    }

    private void checkBoxOilChange_CheckedChanged(object sender, EventArgs e) // Calls total value passing oil_change as an argument.
    {

        total += OIL_CHANGE;                     // if oil change checkbox status changes add oil change value to total
    }


    private void checkBoxLubeJob_CheckedChanged(object sender, EventArgs e) // Calls total value passing LUBE_JOB as an argument.
    {

        total += LUBE_JOB;                                                   /* if lube job checkbox status changes add lube job
                                                                                  value to total */
    }

    private void checkBoxRadiatorFlush_CheckedChanged(object sender, EventArgs e) // Calls total value passing radiator flush as an argument.
    {

        total += RADIATOR_FLUSH;                                                  /* if radiator flush checkbox status changes add radiator flush
                                                                                  value to total */
    }

    private void checkBoxTransmissionFlush_CheckedChanged(object sender, EventArgs e) // Calls total value passing transmission_flush as an argument.
    {
        total += TRANSMISSION_FLUSH;                                                    /* if transmission flush checkbox status changes add transmission flush
                                                                                            value to total */
    }

    private void checkBoxInspection_CheckedChanged(object sender, EventArgs e) // Calls total value passing Inspection as an argument.
    {
        total += INSPECTION;                                                     /* if inspection checkbox status changes add inspection
                                                                                            value to total */
    }

    private void checkBoxMuffler_CheckedChanged(object sender, EventArgs e)   // Calls total value passing replace muffler as an argument.
    {
        total += REPLACE_MUFFLER;                                              /* if muffler checkbox status changes add muffler
                                                                                            value to total */
    }

    private void checkBoxTyreRot_CheckedChanged(object sender, EventArgs e)  // Calls total value passing tyre rotation as an argument.
    {
        total += TYRE_ROTATION;                                               /* if tyre rotation checkbox status changes add tyre rotaion
                                                                                            value to total */
    }

    private void buttonCalculate_Click(object sender, EventArgs e)
    {
            serviceslabour = decimal.Parse(textBoxLabour.Text) + total;
            parts = decimal.Parse(textBoxParts.Text);
            tax = TAX * parts;
            fees = total + parts + tax + serviceslabour;
            labelOutputServiceLabour.Text = serviceslabour.ToString("n2");
            labelOutputParts.Text = parts.ToString("n2");
            labelOutputTax.Text = tax.ToString("n2");
            labelOutputTotalFees.Text = fees.ToString("n2");
    }

1 Answer

Steven Parker
Steven Parker
231,210 Points

:point_right: You can't expect local variables to hold values between separate postbacks.

You didn't post your entire project, and this appears to be server-side code. And I'm guessing that the routines that use the constant values are associated with auto-postback controls on the client side. But you can't expect the value of "total" to be retained between postbacks.

You could possibly use session storage, but an even better approach would be to update a subtotal in a hidden field directly in the client, and submit that with the other inputs when the form is submitted.