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
Best Answer
In the code above
userList
is not an array, it is a mapping. Mapping's do not have alength
property because we can't know what the size of a mapping is.If you convert
mapping(uint256 => address) public userList;
toaddress[] 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.