Data Location Assignment

User storage user can be defined as the instance of an struct, its a variable called user with the structure based on the User struct, and the value already exist in storage, its value will be users[id]

So, we create an instance of an stored values with User storage user = users[id] so we can later modify the storage directly.

Struct instances can be stored/located in memory or storage . It just depends how we want to use them. Storing them in memory isn’t wrong if we don’t need to store their values persistently e.g. if we just need them temporarily to perform some kind of operation within the function.

Carlos Z

1 Like

Basically change line 16 removing “memory” and replacing it with “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;
    }

}
1 Like

Here is my solution for the assignment, I updated the data location to storage instead of memory.

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;
    }

}
2 Likes

It took me an hour to solve this :sweat_smile: I had to review the mapping, struct and data location lessons to start to better understand how all these concepts work together

// Assignment Memory
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 {
         // we save the data permanently with storage, instead of locally with memory
         User storage user = users[id];
         user.balance += balance;
    }

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

This is probably not the cleanest answer because I did not add any exception handling but it does make the balance update. :grinning:

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 memory user = users[id];
        // user.balance = _balance;
         users[id].balance = _balance ;
    }

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

}

2 Likes

niceeeee. the haed work surely does pay off congrats bro

1 Like
//SPDX-License-Identifier: MIT
pragma solidity 0.7.5;
contract MemoryAndStorage {

    mapping(string => uint) users;

    struct User{
        string id;
        uint balance;
    }

    function addUser(uint id, uint balance) public {
        users[id] = User(id, balance);
    }

    function updateBalance(uint _id, uint _balance) public {
         User memory user = users[_id];
         User balance = _balance;
    }

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

}
1 Like

Hopefully, this is correct or partly correct. The complier wanted me to give a storage location for balance, but the balance is a number, and I should not need to write this!

1 Like

No error handling, etc. but does the job…

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

         User memory user = users[id];

         user.balance = balance;

         users[id] = user;

    }
1 Like

i replaced the code from updateBalance function with either one of these two lines, they both work:

function updateBalance(uint _id, uint _balance) public {
users[_id].balance=_balance;
//users[_id]=User(_id, _balance); This one works too.
}

2 Likes

[quote=“thecil, post:1, topic:27990”]
Data Location Assignment

Hi Guys, the solution I used works but was way too easy which leaves me to think that i’m off point. I just inserted “view” in the 'function updateBalance(uint id, uint balance) public view {
Any feedback would be great if I’m way off :slight_smile:

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 view {

     User memory user = users[id];

     user.balance = balance;

}

function getBalance(uint id) view public returns (uint) {

    return users[id].balance;

}

}

1 Like
pragma solidity 0.8.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;
    }

}
1 Like

hey @EtienneW. no your solyution looks fine. sometimes writing code n as most simplistic a mannor is often the nest. often times we think if our code is really complex looking and our file is 100s of lines long, that its better. youll find in production setting sthis is never the case and only confuses your peers. you should always try to come up with a solution that is as easy to read and optimized as possible. this assignment is also one of the easier ones so dont worry if you found it too easy. this does definitely not mean that your solution is bad

1 Like
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;
    }

}

Here is my solution , not sure it’s correct or not , just change from ‘memory’ to ‘storage’

1 Like

Ops, was gonna make a comparison. However, both solutions work. Although, the second one would be better for memory and the first one would be the same as saving it as storage locally in the function?

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract MemoryAndStorage {
mapping(uint256 => User) users;

uint256 newBal;

struct User {
    uint256 id;
    uint256 balance;
}

function addUser(uint256 id, uint256 balance) public {
    users[id] = User(id, balance);
}

function updateBalance(uint256 id, uint256 balance) public {
    newBal = balance;
    users[id] = User(id, newBal);
}

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

}

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract MemoryAndStorage {
mapping(uint256 => User) users;

struct User {
    uint256 id;
    uint256 balance;
}

function addUser(uint256 id, uint256 balance) public {
    users[id] = User(id, balance);
}

function updateBalance(uint256 id, uint256 balance) public {
    users[id].balance = balance;
}

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

}

1 Like
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;
    }

}
1 Like

my change was to make updateBalance access the mapping directly and change the balance value in the struct.

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;
    }

}
1 Like

One way is instead of memory we should use storage data location in updateBalance function:
User storage user = users[id];

[quote="thecil, post:1, topic:27990"]

function formatText(){

let words = “I’m a preformatted Text box, Please use me wisely!”

}
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) view public returns(uint){
     User memory user = users[id];
     return user.balance += balance; 
}
function getBalance(uint id) view public returns (uint) {
    return users[id].balance;
}

}

1 Like