1

The code compiles well. But it gives the following error, when event LogData is activated. LogAddress works well. Do I have to set up explicit getters for the State Variables?

transact to browser/Test.sol:Factory.getContract errored: VM error: invalid opcode. The constructor should be payable if you send value. The execution might have thrown. Debug the transaction to get more information.

pragma solidity ^0.4.15;

contract TestContract {

    uint public iD;
    bytes32 public name;
    function setAttr(uint _id, bytes32 _name) {
        iD = _id;
        name = _name;
    }
}

contract Factory {

    TestContract myTest;
    address contractAddress;
    function Factory () {
        myTest = new TestContract();
        myTest.setAttr(100,"0x111");
        contractAddress = address(myTest);
    }

    event LogData(uint _mydata, bytes32 _name);
    event LogAddress(address _add);
    function getContract() {
        myTest = TestContract(contractAddress);
        LogAddress(contractAddress);
        LogData(myTest.iD(), myTest.name());
    }

}
0

I just fiddled with it until it works.

pragma solidity ^0.4.15;

contract TestContract {

  uint public iD;
  bytes32 public name;
  function TestContract(uint _id, bytes32 _name) public {
    iD = _id;
    name = _name;
  }
}

contract Factory {

  TestContract myTest;
  TestContract testContract;
  function Factory () public {
    testContract = new TestContract(100,"0x123");
  }

  event LogData(uint _mydata, bytes32 _name);
  event LogAddress(address _add);
  function getContract() public {
    LogAddress(testContract);
    LogData(testContract.iD(), testContract.name());
  }
}

Hope it helps.

  • Thanks @RobHitchens, The above changes works now. I had a follow up question: In the LogData, Remix throws an error if I change testContract.iD() to testContract.iD. Do I need to use the default getter? If the state variable is public - shouldn't I be able to access it directly using testContract.iD. Remix shows an error "Type Error: Invalid type of arg. in function call........) – userDSSR Sep 28 '17 at 17:47
  • The "free" 'public' getter for the iD is a function, so it needs () to access. – Rob Hitchens - B9lab Sep 28 '17 at 17:52

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.