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
  • 1
    It would be a good idea to accept the correct answer...
    – bortzmeyer
    Jul 18, 2016 at 9:49

2 Answers 2

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.

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".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.