-2

I am using C# and Visual Studio 2015.

I have two classes BankAccount and Wallet. Wallet class has a transfer method that calls an instance of BankAccount. However, on my form code when I send those parameters, it is not withdrawing funds from the source and destination balances.

transfer method:

public void TransferFund(BankAccount source, BankAccount destination, double amount)
{
    double Source = source.Balance;
    double Destination = destination.Balance;

    if (Source > amount)
    {

        Source -= amount;
        Destination += amount;

    }
    else
    {
        throw new ArgumentException("Insufficient funds for transfer.");
    }

}

on form button click:

                BankAccount from = lbTransferFrom.SelectedItem as BankAccount;
                BankAccount to = lbTransferFrom.SelectedItem as BankAccount;

                Wallet wall = new Wallet();
                double amount = Convert.ToDouble(tbAmount.Text);
                wall.TransferFund(from, to, amount);

Question: How do I get the source and destination objects to actually change the balances as it should?

  • 2
    `Wallet wall = null;` There's your null reference exception – p.s.w.g May 03 '16 at 05:45
  • Didn't you just asked a question on the exact same code? And couple more, yesterday? Why do I feel like you are looking for someone to just write the whole code for you? – jitendragarg May 03 '16 at 05:49
  • jitendra garg: I am learning and these are the aspects I do not know how to do. Considering 90% of my actual code is from my own, no, i'd certainly say I am not. I've also been able to take these things learned and apply them in other learning projects. –  May 03 '16 at 05:54
  • You're not transferring funds, you're just doing math. You need to assign the new balance values back into the accounts they came from before you changed their values. – Lasse V. Karlsen May 03 '16 at 06:19

1 Answers1

2

These two lines

double Source = source.Balance;
double Destination = destination.Balance;

are copying the value of .Balance to a variable. In the following you are then changing the value of those copies of the balance but you are never reassigning the balance of each of the accounts. Instead of copying the values just do

if (source.Balance >= amount) {
    source.Balance -= amount;
    destination.Balance += amount;
} else {
    throw new ArgumentException("Insufficient funds for transfer.");
}

In C# you can divide values into to buckets. Value types and references types. When ever you pass a reference typed variable around, any assignment or other change of state will be reflected by all references ("variables") to the same object. When using value types (most simple types and all structs) every time you assign a variable to another or pass it as a parameter, you are copying the value and passsing the copy along. Any changes to the copy is only reflected in that copy and you'll often find that value types are actually immutable, so in your case both the assignment double Source = source.Balance and Source -= amount are creating a copy and assigning that newly created copy to the Source variable

All that being said I'm expecting this to be a fun project and not for production. If not you need some transactional logic and a more suitable model of an account

Rune FS
  • 21,497
  • 7
  • 62
  • 96
  • I also think it's just a learning project, but if it isn't then I'd also add that someone should put down their foot and be clear that one shouldn't model bank accounts as a `double amount` field, but rather as a ledger of timestamped transactions (and model monetary amounts as ints/decimals, not floats) – sara May 03 '16 at 06:44