3
votes

This method is decreasing value of textBox1 for amount inserted in textBox2. If there isn't enough "money" in textBox1, excessing amount should be taken from textBox3. At the end, method should update textboxes to new values, but only textBox2 is cleared, and textBox1 and textBox3 remain unchanged. Can anyone tell me why textBox1.text = Account.toString() doesn't assign textbox value to value from variable Account and textBox3.text = Savings.toString() doesn't assign textbox value to value from variable Savings?

public void Debit(decimal amount)
    {
        decimal Account = Convert.ToDecimal(textBox1.Text);
        decimal Savings = Convert.ToDecimal(textBox3.Text);

        if ((Account + Savings) < amount)
            if (Overdrawn != null)
                Owerdrawn(this, new OverdrawnEventArgs (Account, amount));
        else if (Account >= amount)
            Account -= amount;
        else {
            amount -= Account;
            Account = 0m;
            Savings -= amount;
        }
        textBox1.Text = Account.ToString();
        textBox2.Clear();
        textBox3.Text = Savings.ToString();
    }
1
have you tried textBox1.Text = String.Format("N0", Account)?Eisenhorn
Please get into the habit of descriptively naming your textBoxes. It'll prevent premature balding for future programmers looking at your code!Michael McGriff
Thanks! It works that way. But still can't figure out why it doesn't work using toString() :/ @MichaelMcGriff Sry, I'll keep that in mindMarko

1 Answers

5
votes

The only way you'd see those results is if you ran into the overdrawn case. If the Account + Savings < amount - you never change the values of Account or Savings in that case.

In every other case you are in fact changing those two values and so the text box values would change. I'm pretty sure the code you want for that branch is:

public void Debit(decimal amount)
{
    decimal Account = Convert.ToDecimal(textBox1.Text);
    decimal Savings = Convert.ToDecimal(textBox3.Text);

    if ((Account + Savings) < amount)
    {
        if (Overdrawn != null)
            Owerdrawn(this, new OverdrawnEventArgs (Account, amount));

        Account = (Account + Savings) - amount;
        Savings = 0m;
    }

    ...

    textBox1.Text = Account.ToString();
    textBox2.Clear();
    textBox3.Text = Savings.ToString();
}