Enum in Solidity

Enum in Solidity
In Solidity, an
enum(short for enumeration) is a user-defined type that represents a set of named values.It is often used to define a list of possible options or choices that can be used as input or output for a contract function.
An
enumis defined using theenumkeyword, followed by the name of theenumand the list of possible values, separated by commas and enclosed in curly braces. For example:
enum State { CREATED, IN_PROGRESS, COMPLETED }
In this example, we've defined an
enumcalledStatethat has three possible values:CREATED,IN_PROGRESS, andCOMPLETED. Each value is assigned an integer value starting from 0, withCREATEDbeing assigned the value of 0,IN_PROGRESSbeing assigned the value of 1, andCOMPLETEDbeing assigned the value of 2.Once an
enumis defined, it can be used as a type for variables and function arguments. For example:
State public currentState;
function setState(State newState) public {
currentState = newState;
}
In this example, we've defined a public variable
currentStateof typeState, which can hold one of the three possible values defined in theStateenum. We've also defined a functionsetStatethat takes an argumentnewStateof typeState, which can be used to update the value ofcurrentState.Using an
enumcan make your code more readable and maintainable by clearly indicating which values are valid inputs or outputs for a function or variable. It can also help catch errors at compile time by preventing invalid values from being used.



