4

I have a very simple contract as below:-

contract Test{
    address public creator;

    function Test(){
        creator = msg.sender;
    }

    function get1() returns (address){
        return creator;
    }
}

When I call test.creator(), I get the address back but when I call the test.get1(), I get an "invalid address" error message.

Can we not return addresses back?

  • 1
    It would be a good idea to accept the correct answer... – bortzmeyer Jul 18 '16 at 9:49
4

You need to have your eth.defaultAccount set up , right now it might have been not set,due to the fact that get1() is a transnational method and it needs who is invoking it. The automatic public method accessor does not do that,thus you could able to invoke it without problem.

You can do something like at the console.

eth.defaultAccount=eth.coinbase;

or

eth.defaultAccount="0xyoucaccount" // if you know your account address

This will solve the invalid address problem , but you would get another problem when execute the get1() method ,it would return you a transaction hash.

You have two options in cases like this, use event logging, or use a constant function. Pick your scenario. This Article could help.

| improve this answer | |
3

Either call test.get1.call() or add the constantkeyword in the definition of get :

function get1() constant returns (address){
    return creator;
}

The automatic "getter" created by the compiler for the public variable creator is "constant".

| improve this answer | |

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.