Let's suppose I have a simple smart contract defining a person with a couple of attributes (for instance Name and Age) and in another smart contract I have a population (with an array of people). I could have a function to return one person like:

function getPerson(uint _index) view returns(Person){
  return population[_index];

And I'll get something like: 0x9e0BC6DB02E5aF99b8868f0b732eb45c956B92dD (what is a Person' object). But, I'd like to see the attributes of that person. How can I do that? I could make a function like this one (I guess):

function getPersonName(uint _index) view returns(string){
  return population[_index].getName();

But I don't like this solution. I mean, is there any chance to see the attributes of my Person object having this address 0x9e0BC6DB02E5aF99b8868f0b732eb45c956B92dD?

1 Answer 1


A getter function is the only way to read a variable from another contract. The Solidity compiler automatically creates getter functions for public state variables. So if the name variable of the Person contract is public you can simply use population[_index].name(), no need for a separate getName() function.

If you have the address of a smart contract, you can access its functions by casting the address to the smart contract type, for example:



function getPersonName(address _personAddress) view returns(string) {
    return Person(_personAddress).getName();

I don't have all your code, but you might be overstretching the analogy of smart contracts being like OOP classes. A struct Person might be more useful than a contract Person.

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.