Member-only story

Solidity Tutorial: all about Enums

Jean Cvllr
9 min readAug 1, 2019

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

  1. How and When to create Enums?
  2. Enums explained with a card deck
  3. Accessing an Enum value
  4. Modifying an enum value
  5. Transition through different Enums values
  6. Using Enums with modifiers
  7. Represent Enums as Strings
  8. Using Hash Functions with Enums
  9. Use Enums as a KeyType in Mappings
  10. 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).

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Jean Cvllr
Jean Cvllr

Written by Jean Cvllr

Smart Contract engineer at @LUKSO. Full Stack Developer. https://github.com/CJ42

Responses (4)

Write a response

choice;

should be `status` here I think

--

Awesome writeup

--

Thank you Jean. I need a clarification please:
From your explanation, I learnt that keccak256() only takes in bytes as an input but with this, I noticed it took my string input parameter with no error. Any explanation?
function…

--