在C#中更新全局变量

时间:2013-05-11 15:38:39

标签: c# variables global

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

    }

如何确保我的余额变量全局更新?

2 个答案:

答案 0 :(得分: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;

}

答案 1 :(得分:0)

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

int bal = Form3.balance;

您正在使用bal Form3.balance的{​​{1}}值。平衡的任何更新都不能在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;
}

请注意

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