Data Location Assignment

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

The ‘User memory user = users[id]’ creates a new temporary variable called user with a copy of the information stored at users[id]. updating the balance of user now just updates the balance of the temporary variable, not the balance of the state variable users. My solution was just to reference the state variable directly.

1 Like

I just changed memory to storage and it worked but I am looking forward to Fillip’s answer.
Probably reducing to one line like
users[id] =User(id, balance);

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

1 Like

I just changed the user in the updateBalance() function from memory to storage.

pragma solidity 0.8.2;

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

Replaced “memory” 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

function updateBalance uses memory so it’s a local variable, it will not persist when the function is finished.
Changed the updateBalance function so it updates the given balance for the user with given id

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

I was thinking that the user[id] was not available to any other functions because this data only gets stored within the updateBalance so therefore I changed the memory syntax to storage to make it available to the whole contract.

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
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
    function updateBalance(uint id, uint balance) public {
        // User memory _user = users[id]; // this memory isn't helping us
        // _user.balance = balance;
        
        users[id].balance = balance; // this is a slick little line of code, I like it
        // I love the mapping[key].structVariable call, so good
    }

@filip – if you’re still looking at all these solutions—you’re a rad teacher

1 Like

i just replaced memory with storage and it worked… still confused why i shouldnt be typing _id and _balance as ive seen fillip doing it in almost every video…

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

hello, here is my solution : (i added a return to the addbalance to show the new balance right after updating it)

pragma solidity 0.8.2;

contract MemoryAndStorage {

struct User{
    uint id;
    uint balance;
}

mapping(uint => User) users;

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

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

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

}

1 Like

“still confused why i shouldnt be typing _id and _balance as ive seen fillip doing it in almost every video…”

it just is a naming scheme, many developers use the underscore to notify that this variable is a input.

2 Likes

Just as @Sajuuk06 suggested, the _id and _balance naming scheme helps delineate between input variables for a function and state variables. That way you can have a state variable (that lives in storage) named balance and use the same word in your functions, but the underscore clarifies which is which.

Filip doesn’t do it every single time, but it isn’t required for the code to compile correctly. Just a preference thing, like the secondWord capitalization in naming functions, e.g. addBalance and depositedTo. Over the years these have become standard naming schemes most programmers adhere to for the sake of clarity & readability.

5 Likes
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
    {
        //User memory user = users[id];     //not necessary
        //user.balance = balance;                //wrong equation
        users[id].balance = balance;          //need to update the global array element users[id]
    }
    
    function getBalance(uint id) public view returns(uint)
    {
        return users[id].balance;
    }
}
1 Like

Nice and correct answer @Hudson bravo!

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{
   User storage user = users[id];
    user.balance = balance;
}

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

}

}

1 Like

Hi @cryptoleta

Your code is correct well done.
Next time you post code please check this FAQ that shows you make it as readable as possibile: FAQ - How to post code in the forum

Good job,
Dani

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

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
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)  public view returns (uint) {
        return users[id].balance;
    }
}
1 Like

I did something that for me was very very AWESOME!! LOL :rofl: I just changed “memory” into “storage” and it worked!! HAHAHA here’s the code:

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

}

I still can’t get over this HAHAHAHA, I tried removing “memory” first then changing user.balance to users.balance but it was wrong. Cool assignment!

1 Like