In Solidity, you can increase the size of an array to make room for a new member by using array.length++. But I'm getting an error:
Value must be an lvalue
In Solidity, you can increase the size of an array to make room for a new member by using array.length++. But I'm getting an error:
Value must be an lvalue
You can resize a dynamic array in storage (i.e. an array declared at the contract level) with “arrayname.length = ;” But if you get the “lvalue” error, you are probably doing one of two things wrong. You might be trying to resize an array in memory, or You might be trying to resize a non-dynamic array.
int8[] memory somearray; // CASE 1
somearray.length++; // illegal
int8[5] somearray; // CASE 2
somearray.length++; // illegal
IMPORTANT NOTE: In Solidity, arrays are declared backwards from the way you’re probably used to declaring them. And if you have a >=2D array with some dynamic and some non-dynamic components, you may violate #2 and not understand why. Note also that arrays are accessed the “normal” way. Here's are some examples of this "backward" declaration paradigm in action:
int8[][5] somearray; // This is 5 dyn arrays, NOT a dyn array-of-arrays w/len=5
// so...
somearray[4]; // the last dynamic array
somearray[1][12]; // the 13th element of the second dynamic array
// thus...
somearray.length++; // illegal. This array has length 5. Always.
somearray[0].length++;// legal
Encountered same issue and what I had to was use the storage
keyword since I was trying to modify a global storage array.
bytes32[] storage someArray = someGlobalStorageArray;
© 2022 - 2024 — McMap. All rights reserved.