0

I am new to solidity.. please bear with my foolish question

I would like to get the variable called num in a parent contract from its children.

So, what I have in my mind is to try to get the address of parent contract first, then I will produce the instance to gain the num in the parent contract.

pragma solidity ^0.6.0;

contract parent{  
    uint public num;  
    function setValue(uint _value) public{  
        num= _value;  
    }  
    function getValue() public view returns(uint){  
        return num;  
    }  
}  
contract child is parent{    
    function get_address() public view returns(address){  
        return address(parent);  // here I have got an error... 
    }  
}  

would you guys give me some advice ?

1
  • Inheritance in solidity doesn't work the same way that in other languages. In the example child and parent have the same address. Since child inherit from parent it can invoke its functions. – Ismael Jan 23 at 19:54
0

I got confused in the explanation, but I believe what you're trying to achieve is reading and updating the state of the parent contract from child contract. Here is how you can achieve it:

pragma solidity ^0.6.0;

contract parent {  
    uint public num;
    
    function setValue(uint _value) public {  
        num = _value;  
    }
    
    function getValue() public view returns(uint) {  
        return num;  
    }  
}


contract child {
    parent parentInstance;
    constructor(address _parent_address) public {
        parentInstance = parent(_parent_address);
    }
    
    function getParentValue() public view returns(uint){  
        return parentInstance.getValue();
    }  
    
    function setParentValue(uint _child_value) public {
        parentInstance.setValue(_child_value);
    }  
}

The difference in my example is that in the child contract I've created instance of the parent contract and now you can refer to parents contract state. This is achieve by passing the address of the parent contract inside the constructor of child contract.

Additional logic, how to pass the address automatically to child contract:

pragma solidity ^0.6.0;

contract parent {
    child public childContract;
    uint public num;
    
    constructor() public {
        childContract = new child(address(this));
    }
    
    function setValue(uint _value) public {  
        num = _value;  
    }
    
    function getValue() public view returns(uint) {  
        return num;  
    }  
}


contract child {
    parent parentInstance;
    constructor(address _parent_address) public {
        parentInstance = parent(_parent_address);
    }
    
    function getParentValue() public view returns(uint){  
        return parentInstance.getValue();
    }  
    
    function setParentValue(uint _child_value) public {
        parentInstance.setValue(_child_value);
    }  
}
3
  • Thank you so much :), is there some way to gain the parent address automatically, not putting the address manually. I mean once the parent contract is deployed, I need to put the parent address into the child contract by my hands. So, I am just wondering if the address of the parent contract can be put into the child contract automatically once the parent one is deployed? – DHC Jan 24 at 6:35
  • Automatically in solidity this can be done only by the time when parent contract is deploying. This can be achieved by creating constructor in parent contract which is creating the child contract and passing the address dynamically as parameter. I edited my answer and included this logic, please take a look. If you find this helpful please give the answer upvote and mark it as selected answer so other struggling with the same issue can see the answer. :) – Miroslav Nedelchev Jan 24 at 6:50
  • Thank you so much :))) – DHC Jan 25 at 12:05

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.