Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6840301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:49:36+00:00 2026-05-26T23:49:36+00:00

I have a problem that is very confusing for me. In the constructor of

  • 0

I have a problem that is very confusing for me. In the constructor of my class I create an instance of SeatManager.cs which holds two arrays (string and double). In the method btnReserveCancel_Click I’m trying to fill these two arrays with data. But when I later call the UpdateGUI() method another instance of SeatManager.cs is created (my teacher added that line of code when I needed help with another thing) and when that happens the all the data I just filled in the two arrays get lost! The wierd part is that if I delete the line which creates the new instance in UpdateGUI() the compiler yells at me that something is wrong.

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);
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:49:37+00:00Added an answer on May 26, 2026 at 11:49 pm

    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?

    Edit: When you declare seatMngr try to also make the instantiation and remove it from the constructor:

    private SeatManager seatMng = new SeatManager(totalNumOfSeats);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have very simple problem. I need to create model, that represent element of
I have an issue that seems like very flaky behavour, is this a problem
I have a very simple problem and a solution that will work, but I'm
Im having a very strange problem, i have a complicated view that returns incorrect
I have a layout that is working, but it has one very annoying problem..
I am having a very confusing problem with an application I have been working
In a couple of scripts that I use I have problem that is intermittent.
I have a problem that confuses my users, being that although an item is
I have the problem that an specific step in Ant can only be executed
I have a problem that I would like have solved via a SQL query.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.