Hi @KennethJP,
Yes there is… In fact you can remove your 2nd and 3rd lines in the updateBalance function, and your 1st line on its own will still produce the same result…
function updateBalance(uint id, uint newBalance) public {
users[id] = User(id, newBalance);
}
But this makes the updateBalance function exactly the same as the addUser function.
While this does still work…
So, instead of the 1st line of code in your solution, you can start with your 2nd line…
The local variable user
creates a temporary “bridge” to the mapping during execution of the function, so that the newBalance
can be added to a specific user’s balance stored persistently in the mapping, before this “bridge” is lost when the function finishes executing.
So, you then want to assign newBalance
to the balance
property of the pointer you’ve just created, as follows:
user.balance = newBalance;
… and this will update the user’s balance in the mapping.
What you’ve done is re-assigned the balance property of the pointer itself to another local variable (which shouldn’t have the same name as your parameter newBalance
, anyway).
Let me know if anything is unclear, or if you have any questions