🎓 A1Learner
🔍
0 of 50 lessons completed

Solidity Enums

An enum is a type whose value can be only one member from a fixed named set. It suits data that moves through a small list of known states.

A fresh enum starts at the first member

The Parcel contract never sets its status. It still reads as Packed, the first member, with number 0.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

contract Parcel {
    enum Status {
        Packed,
        Shipped,
        Delivered
    }

    Status public status;

    function statusNumber() public view returns (uint8) {
        return uint8(status);
    }
}

Functions move the enum to the next state

Each function assigns a later member to the status variable. The parcel moves from Packed to Shipped to Delivered.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

contract ParcelTracker {
    enum Status {
        Packed,
        Shipped,
        Delivered
    }

    Status public status;

    function ship() public {
        status = Status.Shipped;
    }

    function deliver() public {
        status = Status.Delivered;
    }

    function statusNumber() public view returns (uint8) {
        return uint8(status);
    }
}

Members count from 0

Converting each member to uint8 shows its position. Packed is 0, Shipped is 1, and Delivered is 2.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

contract StatusNumbers {
    enum Status {
        Packed,
        Shipped,
        Delivered
    }

    function numbers() public pure returns (uint8, uint8, uint8) {
        return (uint8(Status.Packed), uint8(Status.Shipped), uint8(Status.Delivered));
    }
}

Example

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

contract ParcelJourney {
    enum Status {
        Packed,
        Shipped,
        Delivered
    }

    Status public status;

    function ship() public {
        status = Status.Shipped;
    }

    function deliver() public {
        status = Status.Delivered;
    }

    function statusNumber() public view returns (uint8) {
        return uint8(status);
    }
}

Key points

  • Enum: a type with a fixed set of named members, written once in the declaration.
  • Member: one named choice inside an enum, such as Packed or Shipped.
  • Default value: an enum variable starts at the first member.
  • Moving state: assigning a different member moves the variable to that state.
  • Member numbers: members count from 0 in declaration order.
  • Conversion: uint8(status) turns an enum value into its member number.

In this example, the ParcelJourney contract declares a Status enum with three members. A fresh parcel starts at Packed, so its status number is 0. The ship function assigns Shipped and the number becomes 1. The deliver function assigns Delivered and the number becomes 2.

Note

an enum value is a name from the set, not a string, even though the name reads like a word.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Remix IDE

Exercise

In enum Status { Packed, Shipped, Delivered }, what is uint8(Status.Shipped)?