0

If I set the values of an array as follows, am I wrong?

uint256 n = 0;
bytes32[n] example;

function getArraySize(uint256 arraySize) public returns(uint256 size) {

 size = arraySize;
 return size;

}

n = getArraySize(3);
example[n] = [bytes32("value_1"),bytes32("value_2"),bytes32("value_3")];

I get "parser error: expected identifier" for n = getArraySize(3); but I don't know what is wrong?

Note: This question is related to another question (the link bellow) where in its answer in note number 2, it is mentioned that in case of defining an array with dynamic size, it is not possible to set values of that array like this:

newObject(100, 3, [bytes32("location"),bytes32("price"),bytes32("sold")], [bytes32("Paris"),bytes32("50"),bytes32("yes")], address(0xE07b6e5a2026CC916A4E2Beb03767ae0ED6af773);

Related question link: Error Encoding Argument (remix)

  • Why don't you try to compile this and see what happens? – goodvibration May 4 '18 at 12:23
  • @goodvibration I get "parser error: expected identifier" for n = getArraySize(3); but I don't know what is wrong? – Questioner May 4 '18 at 12:28
0

Your code has many problems. First of all the way you are assigning the size of the array would result in 0 since n starts of as 0 the array size would be 0. Instead create a dynamic array bytes32[] example; would suffice. The next issue is that you are trying to use n = getArraySize(3); example[n] = [bytes32("value_1"),bytes32("value_2"),bytes32("value_3")]; outside of a function which isn't possible with solidity. To fix this you must wrap a function around the code. Afterwards you can use example.push("value_1"); ect to insert into the array. which makes using getArraySize and n irrelevant.

  • Thank you, but according to answer bellow, apparently in solidity we cannot set values of an array like "[bytes32("value_1"),bytes32("value_2"),bytes32("value_3")];" in case the array size is defined dynamically, Do you have a solution for this problem ? Thanks ethereum.stackexchange.com/questions/47538/… – Questioner May 4 '18 at 12:47
  • I literally told you, use example.push("value_1"); example.push("value_2"); example.push("value_3"); – Gabe May 4 '18 at 12:50
  • Please see my function newObject in question I mentioned its link in my previous comment. My problem is how to set values of parameters of bytes32[] sub_states_types and bytes32[] sub_states_values at time of invoking "function newObject". Do you have a solution for how to invoke this function since its parameters are array. Thanks again. – Questioner May 4 '18 at 12:57
  • You can't reference another question on this question wanting an answer for that as well. If this is the solution for this question you can mark it as done and then I can maybe look at your other question. These questions don't relate to each other. – Gabe May 4 '18 at 13:02
  • I did in my question. If I must do some thing else, please let me know. Thanks – Questioner May 4 '18 at 13:15

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.