0

我有一个变量,我要从这段代码中访问它。变量是: 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.
        }

    }

我该怎么做才能确保我的余额变量在全球范围内更新?

4

2 回答 2

1

int是值类型。当你做任务时:

int bal = Form3.balance;

你正在投入价值bal的副本Form3.balance。除非您明确执行,否则任何对余额的更新都无法在 bal 中自动更新。换句话说,修改Form3.balance对 bal 变量没有副作用。

您可以将 balance int 值包装在一个类中,并通过方法或属性将其公开。

public class BalanceWrapper
{
   public int Balance {get;set;}
}
public static BalanceWrapper balance;  

//-------

BalanceWrapper bal = Form3.balance;
public int Balance()
{
    //MessageBox.Show("Your current balance is: "+bal);
    return bal.Balance;
}

笔记

我的只是对什么不起作用的简单解释。就像其他人建议的那样,您可能需要重新考虑您的设计(线程安全在这里可能是一个严重的问题)。

于 2013-05-11T15:44:29.527 回答
0

您应该重构它,以便两种形式都引用一些共享的内部存储类(或持久性机制)。您当前的设计强制在两种形式之间进行不必要的耦合,并且如您所见,实际上并不起作用。这样做的原因是您的内部变量仅在第一次实例化类时设置。您总是可以从其他形式引用静态变量,但这并不能解决耦合问题。此外,您需要担心线程安全,因为两种形式将使用相同的变量,可能同时在不同的线程中。每个表单最好为该值引用一个线程安全的容器。

表格 1

// Use dependency injection to populate the service
private AccountService accountService;
// Not sure how you set the account - this might actually be some global state
private long currentAccount;

private decimal Balance { get; set; }

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
   {
       Account account = accountService.GetAccount(currentAccount);
       account.Deposit(deposit);
       this.Balance = account.Balance;
       MessageBox.Show("Thank you, your balance has been updated.");
   }
}

表格 2

// Use dependency injection to populate the service
private AccountService accountService;
// Not sure how you set the account - this might actually be some global state
private long currentAccount;

private decimal Balance { get; set; }

private void button1_Click(object sender, EventArgs e)
{
    Account account = accountService.GetAccount(currentAccount);

    if (account.Balance < 5)
    {
        MessageBox.Show("Please credit your account before you can withdraw");
    }
    else
    {
        MessageBox.Show("Please wait for your £5 to be dispensed");
        account.Withdraw(5);
    }
    this.Balance = account.Balance;

}

于 2013-05-11T15:43:36.933 回答