2013-05-11 100 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; 
} 

注意

礦只是對什麼行不通一個簡單的解釋。像其他人一樣,你可能需要重新考慮你的設計(線程安全可能是一個嚴重的問題)。