Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在此循环中找不到问题_C# - Fatal编程技术网

C# 在此循环中找不到问题

C# 在此循环中找不到问题,c#,C#,我有一个表单,用户输入我保存在数组中的值,但当用户想要取消时,我希望最后一次询问用户是否要继续并取消预订。如果用户最后一次拒绝,我希望程序返回GUI,并将焦点放在包含所有保留行的文本框上,而不是执行取消操作,但我向您显示我编写的代码,它会询问用户是否确定,如果不确定,它仍会删除保留,然后将焦点放在文本框上。我的代码有什么问题 public void Cancelreservation() { int index = lstReservations.SelectedIndex; b

我有一个表单,用户输入我保存在数组中的值,但当用户想要取消时,我希望最后一次询问用户是否要继续并取消预订。如果用户最后一次拒绝,我希望程序返回GUI,并将焦点放在包含所有保留行的文本框上,而不是执行取消操作,但我向您显示我编写的代码,它会询问用户是否确定,如果不确定,它仍会删除保留,然后将焦点放在文本框上。我的代码有什么问题

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

    }         

假设
m_seatMngr.CancelSeat(index)
是实际取消座位的方法,您将调用该方法两次。第二个
if
语句,代码中有六行,是这样的:

if (!m_seatMngr.CancelSeat(index))

。。。而且(考虑到上述假设)这一行很可能会在您显示MessageBox之前取消座位。

假设
m_seatMngr.CancelSeat(index)
是实际取消座位的方法,您将调用该方法两次。第二个
if
语句,代码中有六行,是这样的:

if (!m_seatMngr.CancelSeat(index))
if (!m_seatMngr.CancelSeat(index))
{
    // the rest of your code, which displays the messageboxes
}
。。。而且(考虑到上述假设)这一行可能会在您显示MessageBox之前取消座位

if (!m_seatMngr.CancelSeat(index))
{
    // the rest of your code, which displays the messageboxes
}
这总是在您显示任何消息框之前调用m_seatMngr.CancelSeat


这总是在您显示任何消息框之前调用m_seatMngr.CancelSeat。

此方法只调用一次?您是否检查了断点发生的情况?如何在代码中单步执行?此方法只调用一次?你检查过断点的情况了吗?你是如何插入代码的?啊,因为m_mngr是一个公共bool,它仍然执行取消操作,因为它只检查索引是否在范围内,然后删除它..然后返回真值。谢谢现在我只需要解决它:先生,你是个天才编写了一个新方法,检查座位是否保留,并返回true或false,然后完美地执行主代码。谢谢你的帮助!!!没问题!请务必接受每个问题的答案(不仅仅是这个问题)-这有助于使StackOverflow上的内容更有用。:-)啊,因为m_mngr是一个公共bool,它仍然执行取消操作,因为它只检查索引是否在范围内,然后将其删除..然后返回true。谢谢现在我只需要解决它:先生,你是个天才编写了一个新方法,检查座位是否保留,并返回true或false,然后完美地执行主代码。谢谢你的帮助!!!没问题!请务必接受每个问题的答案(不仅仅是这个问题)-这有助于使StackOverflow上的内容更有用。:-)你是对的!我现在已经修好了,我想我已经瞎了,因为我已经做了半天了!:'(谢谢你的帮助!!你说得对!我现在已经修好了,我想我已经瞎了,因为我已经做了半天了!:'(谢谢你的帮助!)!!