Switch Statement Calculations

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • luvbug67208
    New Member
    • Apr 2010
    • 2

    Switch Statement Calculations

    Hi im trying to create a switch statement which you see below. I created a combo box and Im trying to set constants if certain numbers are selected in the combo box. The error message that I am getting is "cannot implicity convert type 'double to 'string'. How can I set a constat to certain values using a switch statement. I have to calculate the number of hours rentted * the siz of the yacht (which are the constatnts). Thanks for any feedback.

    here are the constats values:
    Code:
    const double YACHT_22 = 95.00;
    YACHT_24 =137.00;
    YACHT_30 = 160.00;
    YACHT_32 = 192.00;
    YACHT_36 = 250.00;
    YACHT_38 = 400.00;
    YACHT_45= 550.00;
    
    
       double.Parse(comboBoxsizeofyacht.Text);
    
    
                switch (comboBoxsizeofyacht.Text)
                {
                    case "22":
                        comboBoxsizeofyacht.Text = YACHT_22;
                        break;
    
                    case "24":
                        comboBoxsizeofyacht.Text = YACHT_24;
                        break;
                    case " 30 ":
                        comboBoxsizeofyacht.Text = YACHT_30;
                        break;
                    case " 32 ":
                        comboBoxsizeofyacht.Text = YACHT_32;
                    case " 36 ":
                        comboBoxsizeofyacht.Text = YACHT_36;
                        break;
                    case "38":
                        comboBoxsizeofyacht.Text = YACHT_38;
                        break;
                    case "45":
                        comboBoxsizeofyacht.Text = YACHT_45;
                        break;
                }
    Last edited by tlhintoq; Apr 24 '10, 04:23 AM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    The error message says it all.
    error: "cannot implicity convert type 'double to 'string'
    You are trying to set the .TEXT of the combo box (IE: a String) to the DECIMAL of value of a number. You can't do that. Convert the number to a string first.


    Code:
    comboBoxsizeofyacht.Text = YACHT_22.ToString();

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      TIP: When you first created your question you were asked to wrap your code with [code] tags.
      [imgnothumb]http://files.me.com/tlhintoq/10jihf[/imgnothumb]
      It really does help a bunch. Look how much easier it is to read now that someone has done it for you. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

      Comment

      • luvbug67208
        New Member
        • Apr 2010
        • 2

        #4
        I do apologize I am new to all this. If it is converted to string, will I still be able to do the calculation? Its in a string format and I'm trying to take the constatnt value, and muliply by the number of hours the Yacht was rented.

        Comment

        • GaryTexmo
          Recognized Expert Top Contributor
          • Jul 2009
          • 1501

          #5
          You can always convert it from a string, back to a double ;)

          Do a google on the conversion you want, it should turn up any information you need. Also, each of the primitive types have a "Parse" and "TryParse" method attached to them, look into those as well.

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            YACHT_22 will always be a number.
            .ToString() is a method that returns a string - but it doesn't alter the original variable it is attached to.
            YACHT_22.ToStri ng() will provide you a string version of YACHT_22 *that one time* on that one line

            Comment

            Working...