调用对象的实例

时间:2011-11-23 15:41:39

标签: c# object new-operator instance

我有一个让我很困惑的问题。在我的类的构造函数中,我创建了一个SeatManager.cs实例,它包含两个数组(字符串和双精度数)。在btnReserveCancel_Click方法中,我试图用数据填充这两个数组。但是当我稍后调用UpdateGUI()方法时,会创建另一个SeatManager.cs实例(当我需要帮助另一件事时,我的老师添加了那行代码)当发生这种情况时,我刚填充的两个数组中的所有数据都得到了丢失!奇怪的是,如果我删除在UpdateGUI()中创建新实例的行,编译器就会对我说错了。

为什么当btnReserveCancel_Click没有时,UpdateGUI()需要一个新的SeatManager.cs实例?为什么当实例变量中有一个可用时,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);
        }
    }

2 个答案:

答案 0 :(得分:1)

如果你有一个引用指向内存中的对象,然后你为它分配一个新的实例,它之前指向的对象就是“丢失”(前提是它没有更多的引用)并最终被垃圾收集。这就是为什么在UpdateGUI()内创建新实例时丢失所有以前填充的数据的原因。

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

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

private SeatManager seatMng = new SeatManager(totalNumOfSeats);

答案 1 :(得分:0)

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

//seatMngr = new SeatManager(totalNumofSeats);

没有注释。