0

I have a basic struct:

struct Test {
  uint256 rank;
}

Stored in a mapping:

mapping(bytes32 => Test) store;

I am trying to decrement the 'rank' of a specific Test from the mapping:

Test storage test = store[key];
test.rank--;

The rank of the corresponding struct in the mapping doesnt change and I cannot figure out why.

I also tried:

Test memory test = store[key];
test.rank--;
store[key] = test;

But none seem to work. Any hint is much appreciated thanks in advance.

  • uint256 cannot be a negative number. Are you setting the rank to something > 0 before decrementing? That's my first thought when looking at your code. – MSwezey Feb 21 '18 at 4:20
  • Yes the initial value is set to 10. – Warren Wan Feb 21 '18 at 4:22
  • 1
    Both of the variants you tried work fine for me. As does the more straightforward store[key].rank--;. Could you share how you're testing this? Are you sure you're making a transaction and that the transaction is succeeding? – user19510 Feb 21 '18 at 4:28
1

That was simple:

struct Test {
    uint256 rank;
}
mapping(bytes32 => Test) public store;

function setStore(uint256 _unt) returns(bytes32){
    bytes32 aux = sha3("i");
    store[aux].rank = _unt;
    return aux;
}
function minusYolo(bytes32 _key) returns(uint256){
    store[_key].rank--;
    return store[_key].rank;

}

Hope it helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.