Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.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#_Object_New Operator_Instance - Fatal编程技术网

C# 调用对象的实例

C# 调用对象的实例,c#,object,new-operator,instance,C#,Object,New Operator,Instance,我有一个问题让我很困惑。在类的构造函数中,我创建了一个SeatManager.cs实例,它包含两个数组(string和double)。在方法btnReserveCancel\u单击中,我试图用数据填充这两个数组。但是当我稍后调用UpdateGUI()方法时,另一个SeatManager.cs实例被创建(我的老师在我需要其他帮助时添加了这行代码),当这种情况发生时,我刚才在两个数组中填充的所有数据都丢失了!wierd部分是,如果我删除在UpdateGUI()中创建新实例的行,编译器会对我大喊大叫,

我有一个问题让我很困惑。在类的构造函数中,我创建了一个SeatManager.cs实例,它包含两个数组(string和double)。在方法btnReserveCancel\u单击中,我试图用数据填充这两个数组。但是当我稍后调用UpdateGUI()方法时,另一个SeatManager.cs实例被创建(我的老师在我需要其他帮助时添加了这行代码),当这种情况发生时,我刚才在两个数组中填充的所有数据都丢失了!wierd部分是,如果我删除在UpdateGUI()中创建新实例的行,编译器会对我大喊大叫,说有问题

为什么UpdateGUI()需要一个新的SeatManager.cs实例,而btnReserveCancel\u Click不需要?当实例变量中有一个可用实例时,为什么UpdateGUI()需要一个新的SeatManager.cs实例

    private double revenue = 0.0;
    private const int totalNumOfSeats = 10;
    private int numOfReservedSeats = 0; //Increases every time a new reservation is made
    const double minLimit = 10;
    const double maxLimit = 50;
    private SeatManager seatMngr;

    public MainForm()
    {
        InitializeComponent();
        InitializeGUI();
        seatMngr = new SeatManager(totalNumOfSeats);//skapar en instans av klassen SeatManager
        UpdateGUI();
    }



    private void btnReserveCancel_Click(object sender, EventArgs e)
    {
        if (rbtnReserved.Checked == true)//Om radiobutton RESERVE är iklickad
        {
            string customerName = string.Empty;
            double seatPrice = 0.0;

            int selection = listBox1.SelectedIndex;
            if (selection == -1)
            {
                MessageBox.Show(string.Format("You must select which seat you want to reserve!"), "Select a seat.", MessageBoxButtons.OK, MessageBoxIcon.None);

            }
            else
            {
                string getSeatNumber = listBox1.SelectedItem.ToString();//Tar första bokstaven i den markerade strängen i listboxen och gör om till index.
                int seatNumber = int.Parse(getSeatNumber.Substring(0, 1));

                bool inputOk = ReadAndValidateInput(out customerName, out seatPrice);
                bool validSeats = CheckVacantSeats();

                if (inputOk && validSeats)
                {
                    if (seatMngr.ReserveSeat(customerName, seatPrice, seatNumber) != true)
                    {
                        var result = MessageBox.Show(string.Format("Do you wish to overwrite reservation? "), "Seat already registered", MessageBoxButtons.YesNo, MessageBoxIcon.None);
                        if (result == DialogResult.Yes)
                        {
                            double amount = seatMngr.GetPaidPrice(seatNumber);
                            MoneyBackWhenCancelOrOverwrite(amount);
                            seatMngr.ReserveSeatOverwrite(customerName, seatPrice, seatNumber);
                            revenue += seatPrice;
                        }
                    }
                    else
                    {
                        seatMngr.ReserveSeat(customerName, seatPrice, seatNumber);
                        numOfReservedSeats++;
                        revenue += seatPrice;
                        if (seatMngr.ReserveSeat(customerName, seatPrice, seatNumber) == true)
                        {
                            MessageBox.Show(string.Format("Det funkade "), "Sfgdfg", MessageBoxButtons.OK, MessageBoxIcon.None);
                        }
                    }
                }
            }
        }
        else if (rbtnCancel.Checked == true)//Om radiobutton CANCEL är iklickad.
        {
            string getSeatNumber = listBox1.SelectedItem.ToString();//Tar första bokstaven i den markerade strängen i listboxen och gör om till index.
            int seatNumber = int.Parse(getSeatNumber.Substring(0, 1));

            var result = MessageBox.Show(string.Format("Do you wish to cancel reservation? "), "Seat registered", MessageBoxButtons.YesNo, MessageBoxIcon.None);
            if (result == DialogResult.Yes)
            {
                double amount = seatMngr.GetPaidPrice(seatNumber);
                MoneyBackWhenCancelOrOverwrite(amount);
                seatMngr.CancelSeat(seatNumber);
                numOfReservedSeats--;
            }
            else { }
        }
        UpdateGUI();
    }



    private void UpdateGUI()
    {
        labelVacant.Text = (totalNumOfSeats - numOfReservedSeats).ToString();//Visar antal ledig platser.
        labelReserved.Text = numOfReservedSeats.ToString();//Visar antal reserverade platser.
        labelRevenue.Text = revenue.ToString();//Visar intäkter.
        labelSeats.Text = totalNumOfSeats.ToString();//Visar totalt antal platser. Värdet är konstant så det kan inte ändras.
        DisplayOptions choice = (DisplayOptions)comboBox1.SelectedIndex;

        string[] strSeatInfoStrings;


        //seatMngr = new SeatManager(totalNumOfSeats);

        int display = seatMngr.GetSeatInfoStrings(choice, out strSeatInfoStrings);
        listBox1.Items.Clear();
        if (strSeatInfoStrings == null)
        {
            listBox1.Items.Add("No seats where found");
        }
        else
        {
            listBox1.Items.AddRange(strSeatInfoStrings);
        }
    }

如果有一个引用指向内存中的某个对象,然后为其分配一个新实例,则该对象先前指向的对象将“丢失”(前提是不再有对该对象的引用),并最终被垃圾回收。这就是为什么在
UpdateGUI()
中创建新实例时会丢失所有以前填充的数据

如果您计划跨方法调用维护状态,那么正确的版本显然没有新的实例化。如果删除该行,将出现什么编译器错误

编辑:当您声明seatMngr时,尝试进行实例化并将其从构造函数中删除:

private SeatManager seatMng = new SeatManager(totalNumOfSeats);

如果有一个引用指向内存中的某个对象,然后为其分配一个新实例,则该对象先前指向的对象将“丢失”(前提是不再有对该对象的引用),并最终被垃圾回收。这就是为什么在
UpdateGUI()
中创建新实例时会丢失所有以前填充的数据

如果您计划跨方法调用维护状态,那么正确的版本显然没有新的实例化。如果删除该行,将出现什么编译器错误

编辑:当您声明seatMngr时,尝试进行实例化并将其从构造函数中删除:

private SeatManager seatMng = new SeatManager(totalNumOfSeats);

您需要发布SeatManager类的代码,否则很难知道发生了什么。目前,我没有看到任何问题,除非当前被注释掉的行:

//seatMngr = new SeatManager(totalNumofSeats);

未注释。

您需要发布SeatManager类的代码,否则很难知道发生了什么。目前,我没有看到任何问题,除非当前被注释掉的行:

//seatMngr = new SeatManager(totalNumofSeats);

未注释。

删除该行时出现的错误是什么?nullreferenceexception未处理?删除该行时出现的错误是什么?nullreferenceexception未处理nullreferenceexception未处理!斯威耶!谢谢你,伙计!我很好奇,为什么它会起作用?为什么在构造函数中实例化对象时出错?我看到的唯一可能性是没有调用构造函数。你还有其他的构造器吗?没有,只有一个。但该构造函数是在创建类时调用的。构造函数中的InitializeGUI()起作用。非常令人困惑。。但再次感谢!如果已解决您的问题,请不要忘记选择“接受答案”。)nullreferenceexception未经处理已解决!斯威耶!谢谢你,伙计!我很好奇,为什么它会起作用?为什么在构造函数中实例化对象时出错?我看到的唯一可能性是没有调用构造函数。你还有其他的构造器吗?没有,只有一个。但该构造函数是在创建类时调用的。构造函数中的InitializeGUI()起作用。非常令人困惑。。但再次感谢!如果已解决您的问题,请不要忘记选择“接受答案”。)nullreferenceexception未处理nullreferenceexception未处理