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);

9

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);

| improve this answer | |

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.