I'm trying to follow the tutorial here and there's a line where an address array players has this done players.length = 0 which issues this error: member length is read-only and cannot be used to resize arrays. I get that only pop and push are allowed as well as assigning a new empty array. I was wondering how to perform that last operation in this context.
Asked
Active
Viewed 971 times
1
reactor
- 135
- 4
1 Answers
1
players.length=0 was allowed before the solidity 0.6.0. After the update length is now read-only member.
There is a delete keyword you can use to delete the players array.
delete players
But the delete should never be use to remove a single element from the array.
Since delete leave the gap between the array.
You can see first example over here.
contract MyContract {
uint[] array = [1,2,3];
function removeAtIndex(uint index) returns (uint[]) {
if (index >= array.length) return;
for (uint i = index; i < array.length-1; i++) {
array[i] = array[i+1];
}
delete array[array.length-1];
array.length--;
return array;
}
}
Haseeb Saeed
- 683
- 4
- 22
delete players? – goodvibration Jul 12 '20 at 05:18delete. – hrkrshnn Jul 12 '20 at 15:30