4

I've read that getters are automatically created when you make a variable public. Can other contracts access these getters or is it only for web3.eth? Also what would be the getter name?

1 Answer 1

8

The getter's name is the variable's name. So in

contract foo {
    int public bar;
}

You can access it through someFooInstance.bar(). It should work the same in both web3.js and solidity (from another contract.) If it's an array (i.e. int[] public bar) or a mapping (mapping (address=>int) public bar) you can access specific elements through someFooInstance.bar(n).

There's no way to automatically create a setter, because a setter anyone can access is probably a bad idea. But there's no obvious way to decide who can and can't access a setter.

3
  • Ok that makes sense. Do you know how a struct would be accessed?
    – ethereal
    Commented Jan 28, 2017 at 15:16
  • Same way. someStruct public bar. You'll get an array of most elements. Some, such as arrays within arrays, recursive structs, and mapping won't be returned if they're inside a struct. Commented Jan 28, 2017 at 16:10
  • Ok so they're not always so useful it seems. Thanks!
    – ethereal
    Commented Jan 28, 2017 at 16:34

Your Answer

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

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