Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: C# Help

  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    13
    Thanks
    3
    Thanked 3 Times in 3 Posts

    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();
                }
    
            }

  • #2
    New Coder
    Join Date
    Jul 2012
    Posts
    13
    Thanks
    3
    Thanked 3 Times in 3 Posts
    never mind

    solution is ==

  • #3
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,776
    Thanks
    41
    Thanked 196 Times in 195 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •