Click here to Skip to main content
15,921,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I'm a c# beginner and need some help with code below. Additional $5 per person exceeded 3 guests will be added to the room charge. I know how to count (6-3)*$5 but doesn't know how to code it.

what im doing wrong?

thanks

What I have tried:

private void roomTypeComboBox_SelectedIndexChanged(object sender, EventArgs e)
       {

           if (roomTypeComboBox.SelectedItem.Equals("Single"))
           {
               totalAmount = 149;
               floorComboBox.SelectedItem = "1";
           }
           else if (roomTypeComboBox.SelectedItem.Equals("Double"))
           {
               totalAmount = 299;
               floorComboBox.SelectedItem = "2";
           }
           else if (roomTypeComboBox.SelectedItem.Equals("Twin"))
           {
               totalAmount = 349;
               floorComboBox.SelectedItem = "3";
           }
           else if (roomTypeComboBox.SelectedItem.Equals("Duplex"))
           {
               totalAmount = 399;
               floorComboBox.SelectedItem = "4";
           }
           else if (roomTypeComboBox.SelectedItem.Equals("Suite"))
           {
               totalAmount = 499;
               floorComboBox.SelectedItem = "5";
           }
           int selectedTemp = 0;
           string selected;
           bool temp = int.TryParse(qtGuestComboBox.SelectedItem.ToString(), out selectedTemp);
           if (!temp)
           {
               MetroFramework.MetroMessageBox.Show(this, "Enter # of guests first", "Error parsing", MessageBoxButtons.OK);
           }
           else
           {
               selected = qtGuestComboBox.SelectedItem.ToString();
               selectedTemp = Convert.ToInt32(selected);
               if (selectedTemp >= 3)
               {
                  int totalGuests += (selectedTemp -3)*5;
               }

           }
Posted
Updated 18-Jun-18 7:22am

Why are you reading a value from the combobox, converting it (correctly) to an integer, then doing it again after that badly?
Try this instead:
C#
int selectedTemp;
if (!int.TryParse(qtGuestComboBox.SelectedItem.ToString(), out selectedTemp))
{
   MetroFramework.MetroMessageBox.Show(this, "Enter # of guests first", "Error parsing", MessageBoxButtons.OK);
}
else
{
   if (selectedTemp >= 3)
   {
      ...
   }
}
But ... when you declare a variabel inside a set of curly brackets, that is how long it lasts - it's called scope
{
   int totalGuests += (selectedTemp -3)*5; // totalGuests is valid here.
}
// totalGuests no longer exists.
At a guess, totalGuests is a class level variable, so your line of code creates a new variable which "hides" the class level one. Remove the int and that will no longer happen:
{
   totalGuests += (selectedTemp - 3) * 5;
}
 
Share this answer
 
Comments
Member 10877592 18-Jun-18 23:03pm    
thank you, everything is working now
OriginalGriff 19-Jun-18 2:58am    
You're welcome!
You're confusing "totalAmount" and "totalGuests".

"Better" names usually help:

- totalChargeAmount
- totalNumberOfGuests
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900