Member-only story
Solidity Tutorial: all about Enums

The word Enum in Solidity stands for Enumerable. They are user defined type that contain human readable names for a set of constants, called member. The data representation for an enum is the same as the one in the C language.
Table of Contents
- How and When to create Enums?
- Enums explained with a card deck
- Accessing an Enum value
- Modifying an enum value
- Transition through different Enums values
- Using Enums with modifiers
- Represent Enums as Strings
- Using Hash Functions with Enums
- Use Enums as a KeyType in Mappings
- What you can’t do with Enums in Solidity?
1. How and When to create Enums?
A classical example of an Enum would be a deck of card, to represent:
> The suits (Spades, Clubs, Diamonds, Hearts).
> The ranks / values (2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, King, Queen, Ace).
enum Value { Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, King, Queen, Ace }
enum Suit { Spades, Clubs, Diamonds, Hearts }
Note 1 : you can’t use numbers (positive or negative) or boolean (true or false in lowercase) as members for an
enum
. However, True and False (Capitalised) are accepted.Note 2 : you do not need to end the enum declaration with a semicolon. The compiler uses the semicolon
;
and curly brackets{}
to determine the scope of the code it’s applied to.- The semicolon determines the end of single instructions or declarations.
- The curly brackets determine the the start and end of arbitrary numbers of instructions or declarations, like functions, modifiers, structs, enums and the contract itself.
An enum needs at least one member to be defined. However, they are useful for defining a series of predefined values (or out of a small set of possible values).