this is my solution.
excuse me I changed a little the function addUser because you can change balance with this function when the id is already in use. I changed a little the code for this function to avoid update the balance for an id that already exist and an id = 0; Thanksss :)))
function updateBalance(uint id, uint balance) public{
User storage user = users[id];
user.balance = balance;
}
I believe the answer is to use the word storage because memory is only used with structs, string ,ect. not with uint types , since that is what we are using for our contract code.
nice…throwing an error message if the certain user id is in use. cool.
pragma solidity 0.5.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 {
// changed from memory to storage type
User storage user = users[id];
user.balance = balance;
}
function getBalance(uint id) view public returns (uint) {
return users[id].balance;
}
}
Solution: changed from memory to storage type in updateBalance()
user on function updateBalance is save in memory so it lose after function executed. fix as
users[id] = user
or users[id].balance = balance
pragma solidity 0.5.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;
}
}
Am I understanding correctly that the following are functionally the same?
users[id].balance = balance;
AND
User user storage = users[id];
user.balance = balance;
no need to update the object in the mapping as the storage keyword maintains the reference?
users[id] = user; <-- don’t need this?
Also used storage instead
function updateBalance(uint id, uint balance) public {
User storage user = users[id];
user.balance = balance;
My solution is to save the balance into the users mapping before ending the updateBalance function. In the original code , is saved only in the user variable that stores only while the function runs , so it is lost when it finishes.
I just added in updateBalance function the following:
users[id].balance = user.balance;
// Full code:
pragma solidity 0.5.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];
user.balance = balance;
users[id].balance = user.balance;
}
function getBalance(uint id) view public returns (uint) {
return users[id].balance;
}
}
pragma solidity 0.5.12;
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];
users[id].balance = balance;
}
function getBalance(uint id) view public returns (uint) {
return users[id].balance;
}
}
pragma solidity 0.5.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 storage user = users[id];
user.balance = balance;
}
function getBalance(uint id) view public returns (uint) {
return users[id].balance;
}
}
Well, I found 2 solutions, not sure if one is preferred over the other.
pragma solidity 0.5.12;
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 {
// solution 1, store in storage instead of memory
// User storage user = users[id];
// user.balance = balance;
// solution 2, store in users mapping since it is a state variable
users[id].balance = balance;
}
function getBalance(uint id) view public returns (uint) {
return users[id].balance;
}
}
Hi Guys
Solution 1: I changed memory to storage and then the update worked.
Solution 2: I copy pasted the body of the addUser function into the updateBalance function. It’s stupid but also worked…
Solution 3: in updateBalance I replace the body with users[id].balance = balance;
Also stupid but worked.
The problem is that these “solutions” not only update the balance, but also add a user, if I add another number in the input field.
Cheers
pragma solidity 0.5.12;
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;
}
}
pragma solidity 0.5.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 storage user = users[id];
user.balance = balance;
}
function getBalance(uint id) view public returns (uint) {
return users[id].balance;
}
}
pragma solidity 0.5.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;
}
}
You need to change “memory” to “storage” on line 16 in the updateBalance function.
…
function updateBalance(uint id, uint balance) public {
User storage user = users[id];
user.balance = balance;
}
…
On: function updateBalance(uint id, uint balance) public {
User memory user = users[id];
user.balance = balance;
}
Change memory to storage so balance can be saved permanently.
function updateBalance(uint id, uint balance) public {
User storage user = users[id];
user.balance = balance;
}
pragma solidity 0.5.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;
}
}
pragma solidity 0.5.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] = User(id, balance);
//user.balance = balance;
}
function getBalance(uint id) view public returns (uint) {
return users[id].balance;
}
}