0

I am new to Solidity and when I tried this code below and try to change the value from the child contract, it did not work.

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.18;

contract Parent {
    uint public number;

    function setNumber(uint _number) internal {
        number = _number;
    }
}
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.18;

import "./parent.sol";

contract Child is Parent {

    function changeNumber(uint _number) external  {
        setNumber(_number);
    }
}

I read about state variables are not able to be changed externally and I saw about using interface from this post Change State Variable Content in Contract A from Contract B

As from above, it is inheritance, so I am not sure, is it also true that using inheritance will not change the state variables of parent contract? Or is there any other methods other than using interface? Any help would be appreciated! Other information if needed: I use Remix IDE and deploy in Remix VM (London).

Before changing the number:

The number I call from parent contract: enter image description here

The number I call from child contract: enter image description here

After changing the number from child contract:

The number I call from parent contract remains 0 enter image description here

The number I call from child contract has changed enter image description here

6
  • Your code is working fine and it is updating the number by calling changeNumber() function from the child contract as expected. can you explain how you are trying to update the number after deploying the contract? Commented Jul 23, 2023 at 8:46
  • When deployed I think in the parent contract, the number is initially 0. Then, when I change the number in child contract, and call the number using getter in parent contract, it stays 0.
    – tony
    Commented Jul 23, 2023 at 9:49
  • how are you changing the number in the child contract? can you edit your answer and post a screenshot showing the way of changing the number? Commented Jul 23, 2023 at 10:57
  • I input the number from child contract. I have edited the post and the provide the screenshots.
    – tony
    Commented Jul 23, 2023 at 13:09
  • I hope @dhsdhsdsk's answer helps you. And if you are trying to interact with the Parent contract from the Child contract then follow the instructions given in the link you posted in your question. Commented Jul 23, 2023 at 14:21

1 Answer 1

1

If you deploy these contracts separately, with Parent at address A and Child at address B (which is what you're doing), they will have nothing to do with each other. Changes at A will not reflect anything at B and vice versa.

The purpose of this example is to show that the Child contract has the function setNumber when it inherits from Parent.

1
  • Thank you for the explanation! Really appreciate it!
    – tony
    Commented Sep 6, 2023 at 7:20

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.