0

I need to split my smart contract because of the size limit, so I created Inheritance. Never did it before. But I got into trouble, I tried to go back to basics and try this code :

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;


contract parent {
  
  uint256 internal id = 9;
  
  function setValue(uint256 _value) public{
      id = _value;
  } 
  
  function getValue1() public view returns (uint256) {
    return id;
  }
}

contract child is parent {
  
   function getValue2() public view returns (uint256) {
        return id;
   }
}

Deployed the above contracts on Remix and my question is:

Why when I set Value on parent contract, it can't be read from child contract.

Why Parent and Child have their respective values with the same function?

Need explanation from expert.

3
  • In a comment you say both contracts were deployed, you should add that detail to the question. Deploying both is creating two unrelated contracts.
    – Ismael
    Commented Oct 27, 2021 at 23:07
  • @Ismael, please copy paste my code above on Remix. You will have two contracts, (both) that's what I mean.
    – Yusnee
    Commented Oct 28, 2021 at 1:08
  • When developing with inheritance you only deploy the child contract.
    – Ismael
    Commented Oct 28, 2021 at 5:01

1 Answer 1

1

From what I can see, it can be read.

If the id variable was declared private and not internal in that case, you'd not be able to access it from child smart-contract

Screenshot from remix - on the contract specified above. Maybe you just had a mistake and deployed parent - instead of child contract.

5
  • I believe the contracts were split into 2 different .sol files, just merged into one for the question redaction. In that case there is no need to deploy parent, child is enough as it will bring in the logic from parent through inheritance in one single contract. However, it won't help you overcome any size limit, see this answer : ethereum.stackexchange.com/a/90803/84305
    – hroussille
    Commented Oct 27, 2021 at 21:03
  • @Nikola. I deployed both. From your screenshot you just deploy the CHILD. Please deploy the parent and change with setValue from PARENT. The children can not read...??
    – Yusnee
    Commented Oct 27, 2021 at 21:04
  • @Yusnee I believe my previous comment answers yours ? (not sure if you get notified as I commented on the answer)
    – hroussille
    Commented Oct 27, 2021 at 21:07
  • @Nikola Okay. just wondering when I deploy and verify the contract I need to deploy them all. BTW thanks for your suggestion and help.
    – Yusnee
    Commented Oct 27, 2021 at 21:10
  • @Yusnee no. With inheritance, you don't have to deploy Parent. Parent's logic will be included in Child contract.
    – hroussille
    Commented Oct 27, 2021 at 21:17

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.