Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
3.22/5 (3 votes)
See more:
Hi! First, I am SO sorry for my long names, I've been overly descriptive in this assignment because I'm still pretty new, and this has a lot going on.

The problem is this: I need my "calculate" button to calculate my equations, then highlight the label with the smaller total. I used the same variable name twice, as I need it twice, but it seems like I told it 1+1=2, then told it 1+1=4, so I understand WHY I have the error. I just don't know how to fix it. I tried brackets {} but it still gives the error because it's still part of the parent scope, and if I move one out of it, the button won't do both things.

The part that is wrong specifically is this:

//HIGHLIGHTING highlight lesser cost in green

            decimal calc5YrTotal1 = Convert.ToInt32(label5YearTotalCostAnswerCar1.Text);
            decimal calc5YrTotal2 = Convert.ToInt32(label5YearTotalCostAnswerCar2.Text);
            if (calc5YrTotal1 < calc5YrTotal2)
            label5YearTotalCostAnswerCar1.BackColor = Color.Green;
            else
            label5YearTotalCostAnswerCar2.BackColor = Color.Green;

calc1YearGasCar1 and calc1YearGasCar2 are the variables used twice. 

********UPDATE********************

I tried renaming them, and I got another error. 
I tried: 

decimal calc5YrTotal1 = Convert.ToInt32(label5YearTotalCostAnswerCar1.Text);
decimal calc5YrTotal2 = Convert.ToInt32(label5YearTotalCostAnswerCar2.Text);
if (calc1YearGasCar1 < calc1YearGasCar2)
label5YearTotalCostAnswerCar1.BackColor = Color.Green;
else
label5YearTotalCostAnswerCar2.BackColor = Color.Green;

I got this error:

An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

Additional information: Input string was not in a correct format.

*****************************************************************************

My ENTIRE code is this:

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 CarCostComparison___AmyCollins
{
    public partial class FormCarCostComparison : Form
    {
        public FormCarCostComparison()
        {
            InitializeComponent();
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void FormCarCostComparison_Load(object sender, EventArgs e)
        {
            //Author: Amy C


        }

        private void buttonCalculate_Click(object sender, EventArgs e)
        {

            //Constants assumed price of gas and miles driven a year

            const decimal constPriceGas = 3.5m;
            const decimal constMilesDriven = 12000;

            //Variables user entered for car 1 and 2

            decimal userEnteredMPGCar1 = 0;
            decimal userEnteredInsuranceCar1 = 0;
            decimal userEnteredPurchasePriceCar1 = 0;
            decimal userEnteredRepairCar1 = 0;

            decimal userEnteredMPGCar2 = 0;
            decimal userEnteredPurchasePriceCar2 = 0;
            decimal userEnteredRepairCar2 = 0;
            decimal userEnteredInsuranceCar2 = 0;

            //Variables calculations

            decimal calc1YearGasCar1 = 0;
            decimal calc5YearCostCar1 = 0;

            decimal calc1YearGasCar2 = 0;
            decimal calc5YearCostCar2 = 0;

            //Input convert

            userEnteredMPGCar1 = decimal.Parse(textBoxMPG1.Text);
            userEnteredInsuranceCar1 = decimal.Parse(textBoxInsuranceCost1.Text);
            userEnteredPurchasePriceCar1 = decimal.Parse(textBoxPurchasePrice1.Text);
            userEnteredRepairCar1 = decimal.Parse(textBoxRepairCost1.Text);

            userEnteredMPGCar2 = decimal.Parse(textBoxMPG2.Text);
            userEnteredInsuranceCar2 = decimal.Parse(textBoxInsuranceCost2.Text);
            userEnteredPurchasePriceCar2 = decimal.Parse(textBoxPurchasePrice2.Text);
            userEnteredRepairCar2 = decimal.Parse(textBoxRepairCost2.Text);

            //Processing calculations 5 year total cost and 1 year fuel cost

            calc1YearGasCar1 = (constMilesDriven / userEnteredMPGCar1 * constPriceGas);

            calc5YearCostCar1 = (userEnteredPurchasePriceCar1 + (calc1YearGasCar1 * 5) + (userEnteredInsuranceCar1 * 5) + (userEnteredRepairCar1 * 5));


            calc1YearGasCar2 = (constMilesDriven / userEnteredMPGCar2 * constPriceGas);

            calc5YearCostCar2 = (userEnteredPurchasePriceCar2 + (calc1YearGasCar2 * 5) + (userEnteredInsuranceCar2 * 5) + (userEnteredRepairCar2 * 5));

            //Output convert 

            label1YearFuelAnswerCar1.Text = calc1YearGasCar1.ToString();
            label5YearTotalCostAnswerCar1.Text = calc5YearCostCar1.ToString();

            label1YearFuelAnswerCar2.Text = calc1YearGasCar2.ToString();
            label5YearTotalCostAnswerCar2.Text = calc5YearCostCar2.ToString();

             //HIGHLIGHTING highlight lesser cost in green

                int calc1YearGasCar1 = Convert.ToInt32(label5YearTotalCostAnswerCar1.Text);
                int calc1YearGasCar2 = Convert.ToInt32(label5YearTotalCostAnswerCar2.Text);
                if (calc1YearGasCar1 < calc1YearGasCar2)
                    label5YearTotalCostAnswerCar1.BackColor = Color.Green;
                else
                    label5YearTotalCostAnswerCar2.BackColor = Color.Green;
            




        }

        private void textBoxMPG1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBoxMake2_TextChanged(object sender, EventArgs e)
        {

        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            //Clear Button

            textBoxMake1.Text = "";
            textBoxModel1.Text = "";
            textBoxYear1.Text = "";
            textBoxMPG1.Text = "";
            textBoxPurchasePrice1.Text = "";
            textBoxRepairCost1.Text = "";
            textBoxInsuranceCost1.Text = "";
            label1YearFuelAnswerCar1.Text = "";
            label5YearTotalCostAnswerCar1.Text = "";

            textBoxMake2.Text = "";
            textBoxModel2.Text = "";
            textBoxYear2.Text = "";
            textBoxMPG2.Text = "";
            textBoxPurchasePrice2.Text = "";
            textBoxRepairCost2.Text = "";
            textBoxInsuranceCost2.Text = "";
            label1YearFuelAnswerCar2.Text = "";
            label5YearTotalCostAnswerCar2.Text = "";

            //Note to self (placeholder)
        }

        private void label5YearTotalCost_Click(object sender, EventArgs e)
        {

        }
    }
}



(Also, knowing how to clear all the boxes in an easier way would be great, but I'm just worried about the error for now)

Any help is much appreciated!!

Amy
Posted
Updated 20-Feb-14 18:47pm
v4
Comments
george4986 20-Feb-14 23:48pm    
u have declared same variable as decimal and int in the same scope,check my code

sir, if you see closely you will find these lines
decimal calc1YearGasCar1 = 0;

And
int calc1YearGasCar1 = Convert.ToInt32(label5YearTotalCostAnswerCar1.Text);

You cannot define same name variable in a particular code block (Event Or Scope)

Solving The Problem
Change name of any of the 2 variables mentioned above.
 
Share this answer
 
v2
Comments
Adelebeth 20-Feb-14 23:57pm    
I realize this now, I'm not sure how to fix the issue, I need the calculate button to highlight the text as well as use the calculation. How do you suggest I fix this?
Adelebeth 21-Feb-14 0:07am    
I tried changing the name of the second (the one currently "int calc..." but then it's not connected to that calculation anymore. Is there a way to rename it to look at the result of that calculation, and then compare it?
Sergey Alexandrovich Kryukov 21-Feb-14 0:13am    
You should not seek for help, just need to use your brain. This is the only way. Forget "rename". Write the code the way it should work. Imagine you did not make your mistake in first place. How the right could would look like? And then, write this code. There is nothing here you might be unaware of.
—SA
Adelebeth 21-Feb-14 0:16am    
I have been staring at the same code errors for 4 hours now. I think my brain has died. At this point, I have tried for a long time, and I have just changed things and created different errors. I have only been in this class for a couple weeks now, and I think I created something too complex for what I currently know.
Sergey Alexandrovich Kryukov 21-Feb-14 0:32am    
Don't mix up "complex" with "messy" or "convoluted", which can be fixed by doing some near work. With "complex", it's not as simple. There is nothing complex here. And you got the answer.
—SA
try this

C#
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 CarCostComparison___AmyCollins
{
    public partial class FormCarCostComparison : Form
    {
        public FormCarCostComparison()
        {
            InitializeComponent();
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void FormCarCostComparison_Load(object sender, EventArgs e)
        {
            //Author: Amy C


        }

        private void buttonCalculate_Click(object sender, EventArgs e)
        {

            //Constants assumed price of gas and miles driven a year

            const decimal constPriceGas = 3.5m;
            const decimal constMilesDriven = 12000;

            //Variables user entered for car 1 and 2

            decimal userEnteredMPGCar1 = 0;
            decimal userEnteredInsuranceCar1 = 0;
            decimal userEnteredPurchasePriceCar1 = 0;
            decimal userEnteredRepairCar1 = 0;

            decimal userEnteredMPGCar2 = 0;
            decimal userEnteredPurchasePriceCar2 = 0;
            decimal userEnteredRepairCar2 = 0;
            decimal userEnteredInsuranceCar2 = 0;

            //Variables calculations

            decimal calc1YearGasCar1 = 0;
            decimal calc5YearCostCar1 = 0;

            decimal calc1YearGasCar2 = 0;
            decimal calc5YearCostCar2 = 0;

            //Input convert

            userEnteredMPGCar1 = decimal.Parse(textBoxMPG1.Text);
            userEnteredInsuranceCar1 = decimal.Parse(textBoxInsuranceCost1.Text);
            userEnteredPurchasePriceCar1 = decimal.Parse(textBoxPurchasePrice1.Text);
            userEnteredRepairCar1 = decimal.Parse(textBoxRepairCost1.Text);

            userEnteredMPGCar2 = decimal.Parse(textBoxMPG2.Text);
            userEnteredInsuranceCar2 = decimal.Parse(textBoxInsuranceCost2.Text);
            userEnteredPurchasePriceCar2 = decimal.Parse(textBoxPurchasePrice2.Text);
            userEnteredRepairCar2 = decimal.Parse(textBoxRepairCost2.Text);

            //Processing calculations 5 year total cost and 1 year fuel cost

            calc1YearGasCar1 = (constMilesDriven / userEnteredMPGCar1 * constPriceGas);

            calc5YearCostCar1 = (userEnteredPurchasePriceCar1 + (calc1YearGasCar1 * 5) + (userEnteredInsuranceCar1 * 5) + (userEnteredRepairCar1 * 5));


            calc1YearGasCar2 = (constMilesDriven / userEnteredMPGCar2 * constPriceGas);

            calc5YearCostCar2 = (userEnteredPurchasePriceCar2 + (calc1YearGasCar2 * 5) + (userEnteredInsuranceCar2 * 5) + (userEnteredRepairCar2 * 5));

            //Output convert

            label1YearFuelAnswerCar1.Text = calc1YearGasCar1.ToString();
            label5YearTotalCostAnswerCar1.Text = calc5YearCostCar1.ToString();

            label1YearFuelAnswerCar2.Text = calc1YearGasCar2.ToString();
            label5YearTotalCostAnswerCar2.Text = calc5YearCostCar2.ToString();

            //HIGHLIGHTING highlight lesser cost in green

            // calc1YearGasCar1 = Convert.ToInt32(label5YearTotalCostAnswerCar1.Text);
            // calc1YearGasCar2 = Convert.ToInt32(label5YearTotalCostAnswerCar2.Text);
            if (calc5YearCostCar1< calc5YearCostCar2)
                label5YearTotalCostAnswerCar1.BackColor = Color.Green;
            else
                label5YearTotalCostAnswerCar2.BackColor = Color.Green;





        }

        private void textBoxMPG1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBoxMake2_TextChanged(object sender, EventArgs e)
        {

        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            //Clear Button

            textBoxMake1.Text = "";
            textBoxModel1.Text = "";
            textBoxYear1.Text = "";
            textBoxMPG1.Text = "";
            textBoxPurchasePrice1.Text = "";
            textBoxRepairCost1.Text = "";
            textBoxInsuranceCost1.Text = "";
            label1YearFuelAnswerCar1.Text = "";
            label5YearTotalCostAnswerCar1.Text = "";

            textBoxMake2.Text = "";
            textBoxModel2.Text = "";
            textBoxYear2.Text = "";
            textBoxMPG2.Text = "";
            textBoxPurchasePrice2.Text = "";
            textBoxRepairCost2.Text = "";
            textBoxInsuranceCost2.Text = "";
            label1YearFuelAnswerCar2.Text = "";
            label5YearTotalCostAnswerCar2.Text = "";

            //Note to self (placeholder)
        }

        private void label5YearTotalCost_Click(object sender, EventArgs e)
        {

        }
    }
}
 
Share this answer
 
v2
Comments
Adelebeth 20-Feb-14 23:53pm    
I removed "int" from both, was that all the changes you made?

I got this error when I ran it: "An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

Additional information: Input string was not in a correct format."
george4986 21-Feb-14 0:19am    
on which line u r getting error?
i just corrected the syntax didn't executed ur code
Adelebeth 21-Feb-14 0:24am    
I figured out WHAT I can't figure out HOW. I need the value created by

calc5YearCostCar1 = (userEnteredPurchasePriceCar1 + (calc1YearGasCar1 * 5) + (userEnteredInsuranceCar1 * 5) + (userEnteredRepairCar1 * 5));

to be compared to the value of

calc5YearCostCar2 = (userEnteredPurchasePriceCar2 + (calc1YearGasCar2 * 5) + (userEnteredInsuranceCar2 * 5) + (userEnteredRepairCar2 * 5));

so, I was using calc5YearCostCar1 and 2 twice.

What I am having a hard time figuring out, is how do I rename the value I get from

calc5YearCostCar1

to use it in my "if" statement?
george4986 21-Feb-14 0:46am    
i have updated the code plz check and send back any errors,i cant run ur code here

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900