1
struct testR{
    bytes32 name;
    int32 count;
}

testR[] public tests;

function createTest(bytes32[] names) external returns (bool success) {
    uint256 len = names.length;
    for (uint8 i=0;i<len;i++){
        tests.push(testR({name: names[i], count: 0}));
    }
    return true;
}

Here's my code. When I deploy this contract to my private network, function createTest just doesn't work, I pass array of converted to HEX strings, then when I call public array "tests" it always returns me 0x and null. If I try to compile and run this contract in Remix using JVM, it gives such error:

undefined errored: VM error: invalid opcode.
invalid opcode  The constructor should be payable if you send value.
The execution might have thrown.
Debug the transaction to get more information. 

I guess there's something wrong with my loop but it seems so simple.

1 Answers1

0

I just rearranged a few things and the code below worked for me. Also make sure you're actually inputting an array or you'll get an error too.

testR[] public tests;
struct testR{
    bytes32 name;
    int32 count;
}


function createTest(bytes32[] names) external returns (bool success) {
    uint256 len = names.length;
    for (uint8 i=0;i<len;i++){
        tests.push(testR(names[i],0));
    }
    return true;
}
thefett
  • 3,873
  • 5
  • 26
  • 48
  • I'm using web3.js to access my contract from local domain. For example, I define var one = web.toHex('string'); Then I call contract.createTest([one]); It returns me transaction hash. Then I start miner, wait for one block to be mined, and call contract.tests(0,1, callback) and it returns me ["0x", e] – Alex Plekhanov Oct 24 '17 at 14:00
  • I think that's just an issue of accessing the array. Here's a better way to do it: https://ethereum.stackexchange.com/questions/3114/calling-public-array-of-structs-using-web3 – thefett Oct 24 '17 at 14:25
  • Thanks for your help. The problem was in passing the arguments from browser's console using web3.js – Alex Plekhanov Oct 26 '17 at 06:21