10

Unable to access to public variable in Test.sol contract from other contract. How to do it?

Test.sol contract

pragma solidity ^0.4.18;

contract Test {
    uint256 public val = 256;
}

My attemps to access variable:

pragma solidity ^0.4.18;

import "./Test.sol";

contract Other {

    address testAddress;

    function setAddress(address _address) {
        testAddress = _address;            
    }    

    function getVal() public returns (uint256) {
        return Test(testAddress).val;
    }
}

Thanks!

1 Answer 1

10

You can use the following Code for it:

pragma solidity ^0.4.18;

contract Test {
    uint256 public val = 256;
}


contract Other {

    Test testContract;

    function setAddress(address _address) {
        testContract = Test(_address);            
    }    

    function getVal() constant public returns (uint256) {
        return testContract.val();
    }
}

You need to define a variable which is of type Test. Than you can get the value of val by calling its getter function val() which is automatically generated due to the public keyword.

EDIT:

If you want to make variable val private and only accessible for the Other contract you should provide a getter function like the following:

function getVal() public returns (uint) {
    return val;
}

Now you can set the permissions for getVal() so that only addresses who may access val will have permissions to read it.

Hope it helps.

EDIT 2:

This is a complete code snippet for keeping the variable val private and only allow Other contract to read it:

pragma solidity ^0.4.18;

contract Test {

    address owner;
    uint256 val = 256;
    address otherContract;

    function Test(){
        owner = msg.sender;
    }

    modifier onlyOwner() {
        require(msg.sender == owner);
        _;
    }

    modifier onlyOtherContract() {
        require(msg.sender == otherContract);
        _;
    }

    function setOtherContract(address _otherContract) onlyOwner {
        otherContract = _otherContract;
    }

    function getVal() onlyOtherContract returns (uint) {
        return val;
    }
}


contract Other {

    Test testContract;

    function setAddress(address _address) {
        testContract = Test(_address);            
    }    

    function getVal() constant public returns (uint256) {
        return testContract.getVal();
    }
}

Try the following steps:

  1. Deploy Test contract
  2. Deploy Other contract
  3. Set testContract in Other contract with the setAddress function
  4. Set the otherContract variable in the Test contract with the setOtherContract function
  5. Call the getVal function in the Other contract.

Now you can also try to call the getVal function in the Test contract directly and you will see, that your call is reverted. This is because after step 4 only the Other contract is allowed to call getVal.

Kind regards

4
  • I've tried to do like you, but i i get VM exception because of revert. Do you have any ideas why this revert was throwed Commented Feb 2, 2018 at 14:01
  • I've edited my answer. Hope things are more clearly now...
    – joffi
    Commented Feb 2, 2018 at 14:23
  • same issue revert.
    – sensei
    Commented May 16, 2018 at 16:28
  • 3
    the answer seems wrong, contract cannot get public value of other contract without method
    – Tony Dang
    Commented Jul 13, 2019 at 10:10

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.