我的问题令我非常困惑。 在我的班子的构造中,我创建了一个有两种阵列(扼杀和翻一番)的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);
}
}