我有一个变量,我要从这段代码中访问它。变量是: balance
这是来自 Form3 的代码片段:
public static int balance = 0;
private void button1_Click(object sender, EventArgs e)
{
int deposit=int.Parse(textBox1.Text);
if (deposit == 0)
{
MessageBox.Show("Please enter a value greater than 0");
}
else
{
balance = balance + deposit;
MessageBox.Show("Thank you, your balance has been updated.");
}
}
现在,当我想存钱时,我希望余额更新,以便当我从另一个表单查看它时,它需要是编辑后的余额(余额更新为他们存入的金额)。我正在努力更新余额,当我在更新的表单中时它可以工作,但是当我在另一个表单中查看它时,它仍然显示余额为 0。
int bal = Form3.balance;
public int Balance()
{
//MessageBox.Show("Your current balance is: "+bal);
return bal;
}
private void button1_Click(object sender, EventArgs e)
{
/*if (bal < 0)
{
MessageBox.Show("You don't have enough cash in your account, please deposit some money before you can continue");
}
else*/
if (bal < 5)
{
MessageBox.Show("Please credit your account before you can withdraw");
}
else
{
MessageBox.Show("Please wait for your £5 to be dispensed");
bal = bal - 5;
Balance();//I thought if I returned the balance variable from a different method that it would still update regardless
//I am struggling making sure that the balance gets updated.
}
}
我该怎么做才能确保我的余额变量在全球范围内更新?