调用对象的实例
我有一个让我非常困惑的问题。在我的类的构造函数中,我创建了一个 SeatManager.cs 实例,它包含两个数组(字符串和双精度)。在方法 btnReserveCancel_Click 中,我尝试用数据填充这两个数组。但是,当我稍后调用 UpdateGUI() 方法时,会创建 SeatManager.cs 的另一个实例(当我需要帮助处理另一件事时,我的老师添加了该行代码),当发生这种情况时,我刚刚填充两个数组的所有数据都会得到丢失的!奇怪的是,如果我删除在 UpdateGUI() 中创建新实例的行,编译器会向我大喊有问题。
为什么 UpdateGUI() 需要 SeatManager.cs 的新实例,而 btnReserveCancel_Click 不需要?当实例变量中有一个可用的 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);
}
}
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您有一个指向内存中对象的引用,然后为它分配一个新实例,则它之前指向的对象将“丢失”(假设不再有对它的引用)并最终被垃圾收集。这就是为什么当您在
UpdateGUI()
中创建新实例时会丢失所有先前填充的数据。如果您计划跨方法调用维护状态,则正确的版本显然没有新的实例化。如果删除该行,会出现什么编译器错误?
编辑:当您声明 SeatMngr 时,尝试同时进行实例化并将其从构造函数中删除:
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:
您需要发布 SeatManager 类的代码,否则很难知道发生了什么。现在,除非当前注释掉的行:
未注释,否则我目前没有看到问题。
You need to post the code for your SeatManager class or it will be hard to know what is going on. Right now I currently don't see an issue unless the line that is currently commented out:
was uncommented.