-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path013-enums-in-solidity.sol
51 lines (42 loc) · 1.21 KB
/
013-enums-in-solidity.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
/**
* Working with enums in solidity
*
* Enums restrict a variable to have only a few
* predefined values. Tha values enumeraed in the
* list are called enums.
*
* With the use of enums it is possible to reduce
* he number of bugs in your code.
*/
contract LearnEnums {
// create an enum list of french fries sizes
enum frenchFriesSize {
LARGE,
MEDIUM,
SMALL
}
// create a variable choice with datatype frenchFriesSize
// frenchFriesSizes now acts as a datatype for creating new
// variables
frenchFriesSize choice;
// setting a default choice
frenchFriesSize constant defaultChoice = frenchFriesSize.MEDIUM;
// switch choice to small
function setSmall() public {
choice = frenchFriesSize.SMALL;
}
// switch choice to large
function setLarge() public {
choice = frenchFriesSize.LARGE;
}
// funcion to get the choice
function getChoice() public view returns (frenchFriesSize) {
return choice;
}
// funcion to get the choice
function getDefaultChoice() public pure returns (uint256) {
return uint256(defaultChoice);
}
}