Do the variables MyStruct.name & MyStruct.balances inherit the public visibility since I assigned results to public?

Is it possible to make MyStruct.balances private while name to be public? I basically want to restrict access to balances only to the caller of the contract.

contract MyContract {
    struct MyStruct {
        bytes32 name;
        mapping (address => uint256) balances;

    Result[] public results;

Is the best way to achieve this:

  1. change Result[] public results > Result[] results
  2. add public getter for results[x].name
  3. add public getter for results[x].balances with modifier to only allow the contract caller to get their own

struct MyStruct is a Type so the idea of visibility doesn't apply. You can't actually store anything in it. You can only cast other variables with that Type. In case it's not clear, there is no variable MyStruct.name.

You can do a few interesting things with the defined struct type.

In the same way that you would declare a uint:

uint myUint;

You can make a Mystruct:

MyStruct myStruct;


MyStruct public myStruct;

Arrays and mappings work with optional visibility:

mapping(bytes32 => MyStruct) public myStructs;

MyStruct[] public mystructs;

All those examples will give you a "free" getter when visibility is public. The arrays and mappings will require arguments for row or key so it knows which row or storage location to return. On the client side, the multiple parts in the return value will be an array. If the struct itself contains more arrays or mappings, they are not returned (because it won't know which row or key is of interest). You have to build your own functions that zero in on something.

If you haven't played around with it yet, Remix is a great tool to tinker with code and see what it does.

Hope it helps.

  • this explains a lot as to why I am not getting a return value on my tests. Thanks for the detailed explanation!
    – The Nomad
    Aug 31 '17 at 6:01

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.