0

Say I have the following contracts

contract Base {
    uint256 public baseProperty;
}

contract Derived is Base {
    function getBaseProperty() public view returns (uint256) {
        return super.baseProperty;
    }
}
  1. Is since baseProperty is public can it be modified from an external contract.

e.g Base(address).baseProperty = 55

1 Answer 1

1

I dont think you can do it from external contract. public here means you can read like this : Base(address).baseProperty() Because the compiler creates a getter function for public variable so you can get it using the generated function baseProperty(). The difference between public and private is that: for private you need to write your own getter function to allow other contracts to read baseProperty, otherwise, no external contract can read it.

To write to baseProperty you need to write a setter function with public or external visibility, for example :

function writeBaseProperty(uint input) public {
  baseProperty = input;
}
1
  • Okay, good I just wanted to make sure that external contracts cant write to memory when a property is public. TBH, I don't even see why private is useful in solidity, I guess it prevents pollution of the global namespace but at the end of the day it seems like syntactic sugar
    – Ritzy Dev
    Commented Jan 8, 2023 at 5:41

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.