Data Location Assignment

Nice solution @Deini-Atakan :ok_hand:
… and welcome to the forum! I hope you’re enjoying the course :slight_smile:

That’s a very good attempt at an explanation… just to clarify…
The users mapping stores instances of each user. Each user instance is based on the User struct, and therefore has an id property and a balance property. So your solution…

… is assigning (saving) the new balance  =>

  =>  to the balance property
        >  of the User instance (mapped to the id input into the updateBalance function)
             >  in the users mapping

I add the storage keyword to solve the problem. I understand why it worked, which is what led me to my decision to add it. Thanks!

Thank you so much @jon_m both for welcoming and explaining what’s going on behind the scenes, now I know it better. I really enjoy the course so much, I almost finished Ethereum Smart Contracts 101 course and I am planning on moving to 201 after I am done with this but I have my final projects going on in Uni so I probably will be able to start it next week, unfortunately :frowning:. The way you guys explain both in the forum and the course is amazing, when I have a question in mind it is answered in the next 10 seconds most of the time :smiley:

1 Like

That’s really great to hear that you’re so motivated and really enjoying the course, Atakan! :smiley:
And thanks for letting us know that our support and explanations here in the forum are really helpful :grin:
I’m sure you will enjoy the challenges of 201 when your busy Uni schedule allows you to get started on it!

1 Like

Hey @darthgawd,

That’s great, but can you post your code so we can see where and how you’ve added it? :wink:

Jon im sorry! im kind of new to this way of learning lol. Here is my code.

pragma solidity 0.7.5;
contract MemoryAndStorage {

    mapping(uint => User) users; //mapping to create an array of 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;
    }

}
1 Like

You guys are really encouraging and informative. Can’t wait for the 201 course :smiley:

1 Like

Nice solution @darthgawd :ok_hand:
… and worth waiting for :wink:

No problem at all… we’re here to guide you through :mage: … and keep you on the straight and narrow :slight_smile:

By the way…

… mappings and arrays are different types of data structures.

I hope you’re enjoying the course so far.

image Preformatted text

changes above code to as below

image and i got correct output

1 Like

I am in enjoying the course! thank you Jon.

And yeah I know that arrays and mappings are different, but that note helps to remember what to compare it to. Mappings remind me of objects in JS… with its key/value store. Just a reminder :smiley: …thanks again Jon

1 Like
pragma solidity 0.7.5;
contract MemoryAndStorage {

    mapping(uint => User) users;

    struct User{
        uint id;
        uint balance;
    }
    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;
    }

}
1 Like

Like in the video the balance was assigned a variable in memory first.
I changed it to set the value and asserted this was happening:

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;
        assert(users[id].balance == balance);
    }

    function getBalance(uint id) view public returns (uint) {
        return users[id].balance;
    }

}

Works…

1 Like

Hi @jon_m,
Thanks so much for the feedback.

Looking at what you mentioned in the feedback, it looks as though I need to learn more about the structure types that I need to declare memory and storage on, and also learn more about pointers.
I have done python which is a little more human readable, so I just need to get to grasp with the Solidity programming language to understand a little more of the syntax.

I appreciate the advice and careful explanations. Hopefully it will help me to be a better developer/programmer for other assignments and in the future. :smile:

1 Like

Hi @crypto-djent76,

I’m glad you found the feedback helpful and informative. Data location, and when to explicitly declare it, is one of the harder concepts to grasp when you are starting to learn Solidity. You are right about what you need to learn more about next, but this will also be a gradual process as these things take time and exposure to click into place and to feel confident about. I gave you a detailed feedback because you had put a lot of effort into your explanation, which shows enthusiasm about going that bit deeper. Your solution itself was well coded. Truely understanding what the code is doing is more complex and, as I mentioned above, takes time :slight_smile:

1 Like

Hi @loso,

Your solution in the updateBalance function is correct, but you have added a state variable to your contract, which is redundant and so should be removed.

Did you mean to add this, or is it just an editing error?

error. Thanks for the response!

1 Like

Thank you @jon_m I definitely feel that by doing more and more, I will understand the concepts. I’m not always good at reaching out for assistance, but I am trying. I feel as though the best way I learn is just experimenting by myself and going over the concepts over and over. However, I love trying to explain how I came up with solutions once I know it well enough. I really appreciate that you thought my solution was well coded, and I hope to do the same justice in the next assignment. I am really loving the Solidity programming so far, and will continue to do my best :slightly_smiling_face:

1 Like
 function updateBalance(uint id, uint balance) public {
         User **storage** user = users[id];
         user.balance = balance;
    }
1 Like

Hey Denis,

… that’s a great learning technique, because trying to explain what the code is doing, and why you coded something the way you did, can help you to realise what you have actually understood, and can also highlight any gaps in your knowledge that you need to focus on and find out more about :muscle:

1 Like

Here is my working solidity code. from my NEW -_- forum user account…

pragma solidity 0.8.1;
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;
    }

}
1 Like