Menu
Coddy logo textTech

Enums

Part of the Fundamentals section of Coddy's Solidity journey. Lesson 44 of 53.

An enum defines a small set of named choices. Declare its members in order, then refer to a member as TypeName.MemberName. The first member has numeric value zero, the next one has value one, and so on. Named choices make a status easier to read than unexplained numbers.

Declare this at contract scope:

enum Phase { Waiting, Packing, Complete }

Use it inside the provided main function:

Phase phase = Phase.Packing;
console.log(uint256(phase));

The declaration lists Waiting, Packing and Complete. Packing is the second member and therefore converts to one. Converting an arbitrary number into an enum can revert when the number is outside its defined range, so use named members here.

Enum members have numeric values starting at zero in declaration order.

challenge icon

Challenge

Easy

Declare enum Mode with Idle, Running and Finished in that order. Choose Running when active is true, otherwise Idle, and print the numeric value.

The tests pass arguments to main in this order: bool active. Each test starts with fresh contract state.

Use console.log for the requested output. Print only the requested values, one per line, with no extra labels unless the task specifies them.

Try it yourself

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
import "forge-std/console.sol";
contract Main {
    function main(bool active) external view {
        // Write your solution here.
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals

Practice on your own: Solidity playground