English 中文(简体)
申请标书
原标题:Calling an instance of an object

我的问题令我非常困惑。 在我的班子的构造中,我创建了一个有两种阵列(扼杀和翻一番)的SeatManager.c。 btnReserveCancel_Click 我试图用数据填补这两个阵列。 但是,当我后来称之为《最新准则》时,又创建了另一个“SeatManager.cs”(我的老师补充说,在我需要帮助时,该法典的行文是另一回事),当出现我刚刚在两个阵列中填满的所有数据时,就会失去! 令人信服的部分是,如果我删除了在《最新准则》中创建新案例的行文,则编辑在我说有些错了。

Why does UpdateGUI() need a new instance of SeatManager.cs when btnReserveCancel_Click doesn t? Why does UpdateGUI() need a new instance of SeatManager.cs when there is one available in the instance variables?

    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);
        }
    }
最佳回答

If you have a reference pointing to an object in memory and then you assign it a new instance, the object it was previously pointing to is "lost" (provided there are no more references to it) and eventually gets garbage collected. That s why you are losing all previously filled data when you create the new instance inside UpdateGUI().

The correct version is obviously without that new instantiation if you plan to maintain state across method calls. What is the compiler error you are getting if you remove that line?

www.un.org/Depts/DGACM/index_french.htm

private SeatManager seatMng = new SeatManager(totalNumOfSeats);
问题回答

你们需要为你们的海特曼格教区张贴该法典,否则将难以知道正在发生什么。 现在我看不到一个问题,除非目前评论的行文:

//seatMngr = new SeatManager(totalNumofSeats);

意见未定。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签