5

I have two related contracts, let's call them Supplier and Producer.

A Supplier can have a State of inactive, active, or kaput, but only active suppliers can be added to a Producer.

So something like this:

contract Supplier {
  enum State { inactive, active, kaput }
  State constant DEFAULT_STATE = State.inactive

  State public status;

  function Supplier() {
    status = DEFAULT_STATE;
  }
}

But doing this doesn't work.

contract Producer {
  function addSupplier(Supplier _supplier) {
    require(_supplier.status == Supplier.State.active);
  }
}

I considered using an Interface but enum is specifically disallowed in an Interface.

How do I expose enum definitions to external contracts without resorting to something ugly like require(uint(supplier.status) == 1);

1 Answer 1

12

Enums defined inside a contract are by default accessible from other contracts. In Producer you can use the expressions:

  • Supplier.State.inactive

  • Supplier.State.active

  • Supplier.State.kaput

Your code is not working because public state variables behave like functions from the perspective of other contracts. Try replacing this line:

require(_supplier.status == Supplier.State.active);

with this:

require(_supplier.status() == Supplier.State.active);

0

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.