2013-05-11 102 views
0

我有一个变量,我可以从这段代码中访问。该变量是:平衡
这是代码的从Form3一个片段:在C中更新全局变量#

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. 
     } 

    } 

我该怎么办表单确保我的平衡变量在全球更新?

+5

你的'Balance'方法*只返回'bal'。它不会做任何事情 - 它当然不会改变“平衡”。你正在调用一个无副作用的方法,并忽略返回值,它总是*一个不好的迹象。就我个人而言,我会尽量避免让一个全局变量开始,说实话。 – 2013-05-11 15:40:18

回答

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; 

}

0

int是值类型。 当你分配:

int bal = Form3.balance; 

你投入balForm3.balance值的副本。任何平衡更新都不能在bal中自动更新,除非您明确地进行更新。换句话说,修改Form3.balance对bal变量没有副作用。

你可以在一个类中包装平衡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; 
} 

注意

矿只是对什么行不通一个简单的解释。像其他人一样,你可能需要重新考虑你的设计(线程安全可能是一个严重的问题)。