user1501127
user1501127

Reputation: 865

Cant find the issue in this loop

I have a form that the user inputs values that i save in an array but when the user wants to cancel i want the user to be asked a final time if the user wants to go ahead and cancel a reservation. If the user declines this final time i want the program to go back to the GUI and focus on the textbox with all the rows of reservations and not do the cancelation but i show u the code i have written and it asks the user if they are sure and if not it still deletes the reservation and then focus on the textbox. What is wrong in my code?

public void Cancelreservation()
{
    int index = lstReservations.SelectedIndex;
    bool checkedindex = m_seatMngr.CheckIndex(index);
    if (checkedindex)
    {

        if (!m_seatMngr.CancelSeat(index))
        {
            if (lstReservations.SelectedIndex == -1)
            {
                MessageBox.Show("You need to select a row.", "Error!",
                MessageBoxButtons.OK,
                MessageBoxIcon.Exclamation,
                MessageBoxDefaultButton.Button1);
                lstReservations.Focus();
            }
            else
            {
                MessageBox.Show("The seat is not reserved! No need to cancel 
                                  reservation.", "Important Query", 
                                  MessageBoxButtons.OK);
                lstReservations.Focus();
            }
        }
        else
        {
            if (MessageBox.Show("Continue to cancel the reservation?", 
                                "Important Query", MessageBoxButtons.YesNo) 
                                == DialogResult.No)
            {
                lstReservations.Focus();                                
            }
            else
            {
                m_seatMngr.CancelSeat(index);
            }
        }
    }

m_seatMngr

         public bool CancelSeat(int index)
    {

        if (m_vacantList[index] == "Reserved") 
        {
            m_nameList[index] = " - ";
            m_priceList[index] = 0;
            m_vacantList[index] = "Vacant";
            return true;
        }
        else
        {
            return false;              
        }

    }         

Upvotes: 1

Views: 130

Answers (2)

Chris
Chris

Reputation: 4681

if (!m_seatMngr.CancelSeat(index))
{
    // the rest of your code, which displays the messageboxes
}

This is always calling m_seatMngr.CancelSeat, before you've even displayed any messageboxes.

Upvotes: 2

Dan Puzey
Dan Puzey

Reputation: 34218

Assuming that m_seatMngr.CancelSeat(index) is the method that actually cancels the seat, you are calling the method twice. The second if statement, half a dozen lines into your code, is this:

if (!m_seatMngr.CancelSeat(index))

... and it seems likely (given the above assumption) that this line will cancel the seat before you even display the MessageBox.

Upvotes: 3

Related Questions