Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-09-2012, 02:09 AM   PM User | #1
alex11
New Coder

 
Join Date: Jul 2012
Posts: 13
Thanks: 3
Thanked 3 Times in 3 Posts
alex11 is an unknown quantity at this point
C# Help

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

Code:
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 is offline   Reply With Quote
Old 11-09-2012, 02:50 AM   PM User | #2
alex11
New Coder

 
Join Date: Jul 2012
Posts: 13
Thanks: 3
Thanked 3 Times in 3 Posts
alex11 is an unknown quantity at this point
never mind

solution is ==
alex11 is offline   Reply With Quote
Old 11-09-2012, 03:29 PM   PM User | #3
alykins
Senior Coder

 
alykins's Avatar
 
Join Date: Apr 2011
Posts: 1,608
Thanks: 37
Thanked 183 Times in 182 Posts
alykins will become famous soon enough
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)
Code:
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;
__________________

I code C hash-tag .Net
Reference: W3C W3CWiki .Net Lib
Validate: html CSS
Debug: Chrome FireFox IE
alykins is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:12 PM.


Advertisement
Log in to turn off these ads.