繁体   English   中英

在 C# 中更新全局变量

[英]Updating a global variable in C#

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

    }

我该怎么做才能确保我的余额变量全局更新?

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

int bal = Form3.balance;

您正在将Form3.balance值的副本放入bal 任何对 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;
}

笔记

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

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

表格 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;

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM