1

I would like to change a variable of type enum of a contract A from another contract B. I am getting an error. Is it not possible to do so? The error is in the second line of the below code...

contract cs{
 ....
........
POD pod = POD(mainContract);
pod.state = st; 

}
3

This is not an enum-specific issue: You cannot modify properties of another contract without a setter function, not even public ones as the keyword public just auto-generates a getter but not a setter for you. E.g.

contract One {
    uint256 public u;
}

contract Other {
    One public myOne = new One();

    function doSomething() public {
        myOne.u = 123; // doesnt work!
    }
}

Instead, you need a setter function:

contract One {
    uint256 public u;

    function setU(uint256 _u) public {
        u = _u;
    }
}

contract Other {
    One public myOne = new One();

    function doSomething() public {
        myOne.setU(123);
    }
}

In case of an enum this translates to:

contract One {
    enum e { ZERO, ONE, TWO }
    e public myE;

    function setE(e _newE) public {
        myE = _newE;
    }
}

contract Other {
    One public myOne = new One();

    function doSomething() public {
        myOne.setE(One.e.ONE);
    }
}

[ This was actually interesting, the enum fields (ZERO, ONE, TWO in my example) are exposed to that other contract, thanks for asking :-) ]

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.