Nice solution @Andre_Mark
The local variable user
is assigned the user’s record (an instance created from the User struct) from the mapping. In the mapping the user’s record is saved in persistent storage, but the copy previously saved in the local variable was stored temporarily in memory. The fact that, previously, the mapping wasn’t updated with the new balance, wasn’t to do with “connection”, but because the new balance was assigned to the temporary local variable and then lost when the function finished executing. It was never assigned to the persistent user’s record in the mapping.
By assigning the user’s record to a local storage
variable, we don’t create a local copy of the user’s record. Instead, we create a local “pointer” which directly references the user’s record in the mapping — a temporary “bridge” to the mapping during execution of the function. Any value assigned to this local “pointer” variable, before the “bridge” disappears when the function finishes executing, is effectively updating the user’s record stored persistently in the mapping.
Also, for your next assignments, please don’t post screen shots, because we can’t copy and paste your code in order to execute and test it, if we need to. Instead, follow the instructions in this FAQ: How to post code in the forum.
Just let me know if you have any questions