First, in updateBalance I changed User memory user to User storage user.
It still didn’t work. I noticed that in header of getBalance it says view public, but in lecture examples it was public view. I flipped those and it worked, but I’m not sure why. They are separate keywords each with its own use.
Your solutions are correct, and it’s great that you are attempting to explain how the code works.
Just to clarify…
The locally created user variable doesn’t reference the User struct. It creates a new instance of the User struct. The User struct is like a blueprint which is used to declare new instances with the same data structure.
As this new instance is marked storage (instead of memory), it creates a direct reference to the instance with the same id key, stored permanently in the users mapping. Therefore, when the new balance is assigned to the user instance, it is also assigned permanently to the instance with the same id stored in the users mapping.
It should work with either view public or public view , so it’s strange that yours didn’t work at first. Either order compiles successfully for me in Remix.
By the way, please add your code by formatting it within the post itself, instead of posting a screen shot. That way it can be copied and pasted into a text editor and executed, which also makes reviewing and debugging easier. To format your code, click on the </> icon in the menu at the top of this forum’s text editor. This will give you 2 sets of 3 back ticks.
```
input your code here
```
If you now input your code between these, you will end up with it nicely formatted. You should always format the code you post, as it is then much clearer and easier to read, and easier for someone else to copy and paste into a text editor for review and execution. It also makes it easier for you to organise your code with the correct spacing and indentation etc. and to spot any errors or copy-and-paste slips
By the way, please add your code by formatting it within the post itself, instead of posting a screen shot. That way it can be copied and pasted into a text editor and executed, which also makes reviewing and debugging easier. To format your code, click on the </> icon in the menu at the top of this forum’s text editor. This will give you 2 sets of 3 back ticks.
```
input your code here
```
If you now input your code between these, you will end up with it nicely formatted
This line on its own in the function body won’t work, as you haven’t assigned the new balance to the user instance stored in the mapping. Can you at least post the complete updateBalance function, so we can see your solution in context?
Change line 16 in the contract to : Userstorageuser = users[id];
changing storage type to storage will create a pointer to storage. any change in user object will be reflected in the storage itself. if we use memory type it creates a new copy of data. If any update is done on copied data, it will not reflect in the original copy.
Just to clarify, it’s the data location that we are changing to storage. and this creates a “pointer” (as you say) to the usersmapping, and so any change to the userinstance will also be reflected in the mapping (and so the change is stored permanently).
I think this is what you were trying to express. Hopefully, that’s made the terminology clearer for you
That’s essentially correct… in this case, the user instance is only held temporarily within the local scope of the function, and so when the function has finished executing it will be lost along with any modifications made to it during execution. This means that the modifications aren’t reflected in the mapping.