54 Replies
Hi I'm following this tutorial on OOP from Microsoft, but for some reason my initialBalance is not being updated with the correct value even though I have the right parameters for the function. I've been looking through the code but can't seem to figure out what is wrong. Can someone help me out please?
Program.cs
I don't see you ever update
initialBalance
Transaction.cs
uhh let me look for that
If you want some value to be updated... you need to update it
am I not updating it with the makedeposit function?
No
Value types get passed by value, copied if you will
Reference types get passed by reference, where updating one updates all
$refvsvalue
Angius#1586
REPL Result: Success
Result: int
Compile: 475.810ms | Execution: 28.738ms | React with ❌ to remove this embed.
that makes sense
Angius#1586
REPL Result: Success
Result: Foo
Compile: 501.797ms | Execution: 45.733ms | React with ❌ to remove this embed.
And for reference types it works
i dont understand where i'm not updating initialBalance though
Well... you never update it
I initialize it with var account = new BankAccount("Josh", 100000);
Yeah
ima try and figure out where i need to update it
Using this constructor
oh
The constructor sets the number and the owner
Doesn't set the balance
so I would do Balance = initialBalance right?
Yep
but it doesn't work for some reason
the tutorial had me delete that when i did this code:
But...
Balance
property doesn't have a setteri see
how would i add the setter to it?
the tutorial made the getter a method
public decimal Balance {
get
{
decimal balance = 0;
foreach (var item in allTransactions)
{
balance += item.Amount;
}
return balance;
}
}
i tried adding set; to the end but it wouldn't work
You seem to store the balance of the account in a kinda hidden way, where you keep the list of all transactions, and calculate balance based on that
So you'd need to create a new deposit transaction
But then we run into issues with your withdrawal and deposit methods, in that they never add the transaction to the list of transactions
They just create a new transaction and peace out
ok i'm trying to make sense of this
i'll try and work on it some more
Sure thing
alright so i kept looking at the microsoft tutorial and all my code was identical to it so I figured maybe it's for an older version of .net that isn't compatible with my version
i'm. not sure if that's the case but anyways i decided to delete the get method inside of balance
and i added a setter to it like you said
and just had simple += and +- for the balance
the output works fine now, but i'm not sure how I would relate this to the list
do you have any ideas on how I would add a calculation to a list?
Well, you're not using a list now
The previous code stored a list of transactions
facts
Some added money, some removed money
Calculating the balance is as simple as iterating over that list and summing up the money added and removed
Setting balance... is not done
You never set it, since the balance is an outcome of calculating the transactions
so to even get to iterating over the list in the first place, I would have to add entries to it that are negative (withdrawal) and positive (deposit)?
yep
alright i'll work on that
thanks for all the help btw
Which you seem to do with
and
how would you know where to set up the functionality to calculate the balance?
i think i have the adding to list functionality figured out, it just ended up being the same thing as the tutorial though lol
but like you said earlier the balance calculation was in a weird place
You have the functionality to calculate balance in the getter of
Balance
it seemsyeah but didn't you say it was in a weird place?
Not really
It's fine to have it in the getter
so then it all comes back to the idea where my setter is messed up
or me not updating the balance
You don't ever update balance
You don't need the setter
You store the values in the list of transactions
Then, the balance getter calculates it on the fly
so this isn't updating the balance?
If it doesn't have a setter, then no, it doesn't
If you want to keep the tutorial's way of storing transactions and calculating balance on the fly, what you need to do, is add a new deposit transaction with the initialbalance value
That's how you store an initial amount of money in the account
im literally so dumb
all the stuff you said makes sense now
and the code works
but i feel like its literally the same stuff i had before like in the tutorial
would you say this is good now?
Yeah, LGTM
thank you very much 🙂
just preference but I'd keep my constructor at the top of the class
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.