...

View Full Version : C# Help



alex11
11-09-2012, 03:09 AM
hi, Visual C# 2008 keeps giving me an error message saying convert to bool, but when i convert to bool it gives another error. The problem is my if statement but im not sure how to fix it.

the lengthInteger = 22 is the problem according to the compiler errors


private void acceptButton_Click(object sender, EventArgs e)
{
try
{
int hoursInteger = 0;
int lengthInteger = 0;
int totalInteger = 0;
int rateInteger = 0;


hoursInteger = int.Parse(hoursTextBox.Text);
lengthInteger = int.Parse(lengthComboBox.Text);

if (lengthInteger = 22)
{
totalInteger = 95 * hoursInteger;
rateInteger = 95;
}
if (lengthInteger = 24)
{
totalInteger = 137 * hoursInteger;
rateInteger = 137;
}
if (lengthInteger = 30)
{
totalInteger = 160 * hoursInteger;
rateInteger = 160;
}
if (lengthInteger = 32)
{
totalInteger = 192 * hoursInteger;
rateInteger = 192;
}
if (lengthInteger = 36)
{
totalInteger = 250 * hoursInteger;
rateInteger = 250;
}
if (lengthInteger = 38)
{
totalInteger = 400 * hoursInteger;
rateInteger = 400;
}
if (lengthInteger = 45)
{
totalInteger = 550 * hoursInteger;
rateInteger = 550;
}

string summaryString = "Orderer Name: "
+ nameTextBox.Text
+ "\n\n" + "Total Hours: "
+ hoursInteger.ToString("N")
+ "\n\n" + "Hourly Rate: "
+ rateInteger.ToString("C")
+"\n\n" + "Total Cost: "
+ totalInteger.ToString("C");
MessageBox.Show(summaryString, "Dinner cost Summary",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);

}

catch (FormatException)
{
MessageBox.Show ("Please enter a whole number");
hoursTextBox.Focus();
}

}

alex11
11-09-2012, 03:50 AM
never mind

solution is ==

alykins
11-09-2012, 04:29 PM
yes comparisons need to be == not =... some other things, you should be using else if instead of if if if if if if assuming you are evaluating the one variable one time (which from the code it does)... also something to look at that would make it easier/cleaner is case statements (although you may not be to them yet, they are not too difficult)... your code would look as follows...(I also condensed your logic some)


switch(lengthInteger)
{
case 22 :
rateInteger = 95;
break;
case 24 :
rateInteger = 137;
break;
case 30 :
rateInteger = 160;
break;
case 32 :
rateInteger = 192;
break;
case 36 :
rateInteger = 250;
break;
case 38 :
rateInteger = 400;
break;
case 45 :
rateInteger = 550;
break;
default :
throw new Exception();
}
totalInteger = hoursInteger * rateInteger;



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum