Nice solution @Mike_C
… and it’s good to see you back here in the forum! I hope you’re enjoying the course
Thanks Jon. It’s been a while, but I’m getting back into it!
Hey guys, to be honest I couldn’t figure it out on my own, still trying to grasp what I’ve learned so far with Filip. I had to sneak into the next video and after a few seconds when Filip mentioned “storage” I could get it. I tried with require and assert but still finding the concepts quite challenging. Need to review the lessons one by one to get the Solidity language more familiar to operate with. Hope I can make progress and be able to solve situations on my own later on
pragma solidity 0.7.5;
contract MemoryAndStorage {
mapping(uint => User) users;
struct User{
uint id;
uint balance;
}
function addUser(uint id, uint balance) public {
users[id] = User(id, balance);
}
function updateBalance(uint id, uint balance) public {
User storage user = users[id];
user.balance = balance;
}
function getBalance(uint id) public view returns (uint) {
return users[id].balance;
}
}
Hi @juanmcba,
That’s fine to start watching the solution video to get some hints, as long as you’ve worked hard at solving it yourself first… which it sounds like you did If you can’t work it out on your own, it’s actually a really good learning technique to try to get some hints and suggestions first, and then try to solve it, before looking at the whole solution.
You may find some of the following learning and study techniques helpful, both when reviewing lessons, and to help you to take a slower and steadier approach when moving through courses.
- Post your work-in-progress (or just part of it) here in the forum, by following the discussion topic link given to you for each section of the course. Include a specific question about a particular problem you are encountering. This can be an effective approach if you would like help before you look at the solution.
- Have a look at other students’ posts in the relevant forum discussion topic, with their attempts, solutions, and the help, comments and feedback posted as replies. There are a lot of comments and explanations posted here. It’s well worth spending your time browsing and reading, not only to get help with the exercises, but also after finishing a section or particular assignment as a way to review what you’ve learnt, and to discover alternative coding solutions or answers.
- When you look at the assignment solutions, if they are different to your code/answers, you should spend some time analysing and thinking about them. This is a really important stage in the learning process. You can learn a lot by working out what the solution code does and how it does it, and also how to go about “bridging the gap” from what you managed to do (or not) on your own. However, it’s important to remember that there are usually several different alternative approaches and solutions to these exercises, so if you’ve coded yours differently, but it still works, then it may well be valid. If you’re not sure, then post it here in the forum, and we’ll review it for you.
- Another good learning technique to use (after having already done the tasks described above) is to then try the exercise again from scratch without looking back at the answer (like a memory test). You can also try to do this after having left it for a certain period of time (say, the next day, then after a few days, then a week etc. etc.) This builds up longer-term retention, and is also a good approach to take when reviewing courses. Instead of just looking back at your solutions to the exercises, test yourself to see how many of them you can redo from scratch — only checking your previous code and notes, and rewatching the videos, if you need to remind yourself about certain things.
- Another important point to remember is that it can take some time before you fully understand all of the code in an exercise, so another good strategy is to come back and revisit an exercise, which you didn’t fully understand, after you’ve progressed a bit more through the course. You may well find that it’s much easier then.
- Don’t forget to do some research yourself on the Internet. Here is a link to a playlist from the YouTube channel Eat The Blocks. I’ve seen some of this guy’s videos myself and I think they are particularly helpful for learning Solidity. The videos are quite short and the explanations clearly explained and well demonstrated with clear examples. You’ll find that several of the videos in this playlist correspond to specific sections of this course, and so serve as an ideal starting point for your own further research.
https://www.youtube.com/playlist?list=PLbbtODcOYIoE0D6fschNU4rqtGFRpk3ea - Finally … play around with the code, experiment, and try to come up with your own examples, no matter how basic, short or simple. Even by just making a few small changes and adaptations to the code presented in the course, any amount of personalisation that you are able to add to your code will help it to mean more to you, and will therefore help you to internalise it better.
Anyway, I hope at least some of these suggestions help make the challenge more manageable, productive and enjoyable
I found this solution, just try to change memory to storage, and it works.
pragma solidity 0.7.5;
contract MemoryAndStorage{
mapping(uint => User) users;
struct User{
uint id;
uint balance;
}
function addUser(uint id, uint balance) public {
users[id] = User(id, balance);
}
function updateBalance(uint id, uint balance) public {
User storage user = users[id];
user.balance += balance;
}
function getBalance(uint id) view public returns(uint){
return users[id].balance;
}
}
Nice solution @Raf.PV7
… and it’s good to see you back here in the forum I hope you’re enjoying this course.
Just one thing …
In Solidity, memory and storage are what we refer to as the data location, not the data access object (DAO).
Sorry for that wrong info, I’ve already corrected the comment.
Thanks a lot for your reply! It makes it clear that is up to me to dig more into it. Now I know how to manage the learning process to better approach Solidity will keep trying to motivate myself with all these suggestions. See you around in the Academy !
pragma solidity 0.7.5;
contract MemoryAndStorage {
mapping(uint => User) users;
struct User{
uint id;
uint balance;
}
function addUser(uint id, uint balance) public {
users[id] = User(id, balance);
}
function updateBalance(uint id, uint balance) public {
users[id].balance = balance;
}
function getBalance(uint id) view public returns (uint) {
return users[id].balance;
}
}
The code was creating a new memory variable and updating it, instead of updating the user balance stored in the contract. The memory variable does not exhist outside the function.
This can be solved directly reassigning the new input balance to the state variable
pragma solidity 0.7.5;
contract MemoryAndStorage {
mapping(uint => User) users;
struct User{
uint id;
uint balance;
}
function addUser(uint id, uint balance) public {
users[id] = User(id, balance);
}
function updateBalance(uint id, uint balance) public {
users[id].balance = balance;
}
function getBalance(uint id) view public returns (uint) {
return users[id].balance;
}
}
function change
pragma solidity 0.7.5;
contract MemoryAndStorage {
mapping(uint => User) users;
struct User{
uint id;
uint balance;
}
function addUser(uint id, uint balance) public {
users[id] = User(id, balance);
}
function updateBalance(uint id, uint balance) public returns (uint) {
User memory user = users[id];
users[id].balance = balance;
}
function getBalance(uint id) view public returns (uint) {
return users[id].balance;
}
}
Nice solution @filbrunelli, with a good accompanying explanation
That’s correct… and in our contract the state variable is a particular User
struct instance stored in a mapping.
Hi @StanleyGM,
In the first line you define a local variable user
, but then you don’t reference it in the second line. So the first line is now completely redundant and can, therefore, be removed.
The second line on its own provides all of the necessary functionality: it assigns the new balance
value to the balance property of a specific User
instance stored in the mapping (users[id].balance
). The User
instance to be updated, is specified by its key: the id
value input into the function.
One other thing… your updateBalance function does not return a value, and so you can also remove returns(uint)
from the function header; or you could add a return statement, although this isn’t necessary, because balances can already be obtained by calling getBalance().
You should be getting compiler warnings (in orange) for both of these pieces of reduntant code. If you look at the warning messages, they describe what the issues are and how to resolve them.
Let us know if anything is unclear, or if you have any questions
mapping(uint => User) users;
struct User{
uint id;
uint balance;
}
function addUser(uint id, uint balance) public {
users[id] = User(id, balance);
}
function updateBalance(uint id, uint balance) public {
User storage user = users[id]; //You need to change memory to storage. Memory is temporary and will therefore delete after the first entry.
user.balance = balance;
}
function getBalance(uint id) view public returns (uint) {
return users[id].balance;
}ype or paste code here
Thanks for the help! I saw the solution in the course and saw the error in my code.
I removed the shallow copy memory variable in updateBalance and acessed the users balance directly
pragma solidity 0.7.5;
contract MemoryAndStorage {
mapping(uint => User) users;
struct User{
uint id;
uint balance;
}
function addUser(uint id, uint balance) public {
users[id] = User(id, balance);
}
function updateBalance(uint id, uint balance) public {
users[id].balance = balance;
}
function getBalance(uint id) view public returns (uint) {
return users[id].balance;
}
}
function updateBalance(uint id, uint balance) public {
User storage user = users[id]; // I use storage instead of memory
user.balance = balance;
}
Like Fillip said: Memory variables are not saved permanently - they are erased once the function completed its execution. While storage variables are permanently saved.
Is this a reasonable explanation for this?
pragma solidity 0.7.5;
contract MemoryAndStorage {
mapping(uint => User) users;
struct User{
uint id;
uint balance;
}
function addUser(uint id, uint balance) public {
users[id] = User(id, balance); //asign the actual object
}
function updateBalance(uint id, uint balance) public {
users[id] = User(id, balance);
/*User memory user = users[id];
user.balance = balance;*/
}
function getBalance(uint id) view public returns (uint) {
return users[id].balance;
}
}
I just changed the memory type in the updateBalance function to storage.
pragma solidity 0.7.5;
contract MemoryAndStorage {
mapping(uint => User) users;
struct User{
uint id;
uint balance;
}
function addUser(uint id, uint balance) public {
users[id] = User(id, balance);
}
function updateBalance(uint id, uint balance) public {
User storage user = users[id];
user.balance = balance;
}
function getBalance(uint id) view public returns (uint) {
return users[id].balance;
}
}
pragma solidity 0.7.5;
contract MemoryAndStorage {
mapping(uint => User) users;
struct User{
uint id;
uint balance;
}
function addUser(uint id, uint balance) public {
users[id] = User(id, balance);
}
function updateBalance(uint id, uint balance) public {
users[id].balance = balance;
}
function getBalance(uint id) view public returns (uint) {
return users[id].balance;
}
}