Enums

Enums are generally used for creating custom types with a limited set of values:

  1. Create an enum in solidity with the enum keyword:

enum Direction {
// Options
}
  1. Enums should contain at least one member:

enum Direction {
North,
East,
South,
West
}
  1. You can initialize enums directly:
Direction path = Direction.North;
  1. Enums support explicit conversion to and from all integer types:
function getCurrentChoice() view public 
returns (uint) {
return uint(path);
}
  1. Use the following contract as an example of enums:
pragma solidity ^0.4.23;

contract Navigation {

// enum declaration
enum Direction {
North,
East,
South,
West
}

// Default direction
Direction path = Direction.North;

// Function which accepts enum as input
function changeDirection(Direction dir) public {
path = dir;
}

// Function which returns enum. Since enum is not part of ABI, return type will be changed to uint
function getCurrentDirection() view public
returns (Direction) {
return path;
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.38.121