user2373154
user2373154

Reputation: 24

Updating a global variable in C#

I have a variable that I am gong to access from this bit of code. The variable is: balance
This is a snippet of the code from 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.");
       }
    }

Now when I want to deposit money, I want the balance to update so that when I view it from another form it needs to be the edited balance (the balance updated with the amount they deposit). I am struggling to get the balance to update, it works when I am in the Form that updates but then when I go to view it in another Form it still shows the balance as 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.
        }

    }

What do I do to ensure that my balance variable is updated globally??

Upvotes: -1

Views: 10401

Answers (2)

tvanfosson
tvanfosson

Reputation: 532705

You should refactor this so that both forms reference some shared internal storage class (or persistence mechanism). Your current design forces an unnecessary coupling between the two forms and, as you see, doesn't actually work. The reason for this is that your internal variable is only set when the class is first instantiated. You could always reference the static variable from the other form, but that won't solve the coupling problem. Also, you need to worry about thread safety since both forms would be using the same variable, potentially at the same time in different threads. It would be better for each form to reference a thread-safe container for the value.

Form 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.");
   }
}

Form 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;

}

Upvotes: 0

Heisenbug
Heisenbug

Reputation: 39194

int are value types. When you do the assignment:

int bal = Form3.balance;

you are putting into bal a copy of Form3.balance value. Any update to balance can't be automatically updated in bal, unless you do it explicitely. In other words, modifying Form3.balance has no side effect on bal variable.

You could wrap balance int value inside a class,and expose it through a method or a property.

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

NOTE

Mine is just a simple explanation on what doesn't work. Like other people suggested, you probably need to rethink of your design (thread safety could be a serious issue here).

Upvotes: 0

Related Questions