Solidity Struct Array – Accessing External Contract’s Struct Array Mappings in Solidity

arrayssoliditystruct

I have struct in an external contract that I need access to

Contract Ext{
 Struct user{
    uint dailybalance;
 }
 mapping ( address => user[] ) userDailyBalances;
}

How do I access the dailybalance for a specific index in the mapped array of structs?

Contract Need{

   function need(){
      Ext x = Ext ( Extcontractaddress );
      uint256 bal = x.userDailyBalances( msg.sender )[0].dailybalance;

  } 
}

Does not work nor does
x.userDailyBalances( msg.sender[0].dailybalance )
nor
x.userDailyBalances( msg.sender.[0].dailybalance )

Best Answer

You are trying to access the storage of the external contract directly which is not allowed. Refer here.

You need to have a getter method implemented inside the external contract to make this feasible.

External Contract:

Contract Ext{
 Struct user{
    uint dailybalance;
 }
 mapping ( address => user[] ) userDailyBalances;

function getDailyBalance(user) constant returns(unit) {
        return userDailyBalances( user )[0].dailybalance;
    }

}

Your Contract:

Contract Need{

   function need(){
      Ext x = Ext ( Extcontractaddress );
      uint256 bal = x.getDailyBalance( msg.sender );

  }

}

Hope this helps.