0
mapping(uint256 => address) public userList;

function getUserCount() public constant returns(uint userCount) {
 return userList.length;
}

function newUser(address _address) internal returns(bool success) {
    userList.push(_address);
    return true;
  }

function getUser(uint256 _userId) public constant returns(address){
      return(userList[_userId]);
  }

throws error "member length can not be found" - can somebody explain to me why this doesn't work? i've looked at the storage patterns list and saw that they would add an additional array to have a user count - i was really trying to avoid that so that on high demand two users would not get the same id, by setting their id to length - was just planning to push the new users without setting an id manually

3

In the code above userList is not an array, it is a mapping. Mapping's do not have a length property because we can't know what the size of a mapping is.

If you convert mapping(uint256 => address) public userList; to address[] public userList; then it should work as expected.

Also, regarding your last point. It's fine for you to use userList.length to determine a user's ID, even if two users are added in the same block/at the same time, as the transactions are processed sequentially, so they will still get different ID's.

  • okay, i think i'm not really getting the concept of arrays in solidity quite yet how would i declare mapping(address => mapping (uint256 => EntityStruct)) as a proper array? – Rainbow Unicorn Jan 30 '18 at 16:07
  • 1
    Mappings and Arrays are fundamentally different data structures. I'm pretty sure you can't represent that Mapping within a Mapping, with those types, as an Array. However, you could represent mapping(uint256 => mapping (uint256 => EntityStruct)) as an Array with EntityStruct[][]. – willjgriff Jan 30 '18 at 16:23

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.