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:
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)
{
}
private void buttonCalculate_Click(object sender, EventArgs e)
{
const decimal constPriceGas = 3.5m;
const decimal constMilesDriven = 12000;
decimal userEnteredMPGCar1 = 0;
decimal userEnteredInsuranceCar1 = 0;
decimal userEnteredPurchasePriceCar1 = 0;
decimal userEnteredRepairCar1 = 0;
decimal userEnteredMPGCar2 = 0;
decimal userEnteredPurchasePriceCar2 = 0;
decimal userEnteredRepairCar2 = 0;
decimal userEnteredInsuranceCar2 = 0;
decimal calc1YearGasCar1 = 0;
decimal calc5YearCostCar1 = 0;
decimal calc1YearGasCar2 = 0;
decimal calc5YearCostCar2 = 0;
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);
calc1YearGasCar1 = (constMilesDriven / userEnteredMPGCar1 * constPriceGas);
calc5YearCostCar1 = (userEnteredPurchasePriceCar1 + (calc1YearGasCar1 * 5) + (userEnteredInsuranceCar1 * 5) + (userEnteredRepairCar1 * 5));
calc1YearGasCar2 = (constMilesDriven / userEnteredMPGCar2 * constPriceGas);
calc5YearCostCar2 = (userEnteredPurchasePriceCar2 + (calc1YearGasCar2 * 5) + (userEnteredInsuranceCar2 * 5) + (userEnteredRepairCar2 * 5));
label1YearFuelAnswerCar1.Text = calc1YearGasCar1.ToString();
label5YearTotalCostAnswerCar1.Text = calc5YearCostCar1.ToString();
label1YearFuelAnswerCar2.Text = calc1YearGasCar2.ToString();
label5YearTotalCostAnswerCar2.Text = calc5YearCostCar2.ToString();
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)
{
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 = "";
}
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