Search code examples
solidityhardhat

Why hasn't this value changed ,use slodity 0.8.6


I use hardhat test my contract,contract code like this:

uint256 public investLength;
    
function A()external{
    uint256 id = investLength++;
    _a(id);
}

function _a(uint256 _Id) internal returns (address) {
   require(_id != 0, 'id zero');
}

when running test,return id zero.This code doesn't seem to be working =>investLength++,this value did not change to 1;


Solution

  • uint256 id = investLength++;
    

    This snippet assigns the current value of investLength (which is 0 by default) to id, and then increments investLength.

    If you want to increment the investLength first, and then assign the already incremented value to id, use this expression:

    uint256 id = ++investLength;