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?


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.

  • Ok that makes sense. Do you know how a struct would be accessed? – ethereal Jan 28 '17 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. – Matthew Schmidt Jan 28 '17 at 16:10
  • Ok so they're not always so useful it seems. Thanks! – ethereal Jan 28 '17 at 16:34

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.