#100DaysOfSolidity ๐Ÿ“š Solidity Enum: Modeling Choices and State Tracking ๐Ÿš€ #14

#100DaysOfSolidity Series 014 โ€œEnumโ€

Solidity Academy
4 min readJul 2, 2023

Solidity, the popular programming language for Ethereum smart contract development, offers a powerful feature called enums. ๐ŸŒŸ Enums allow developers to model choices and track state in a structured and efficient manner.

#100DaysOfSolidity ๐Ÿ“š Solidity Enum: Modeling Choices and State Tracking ๐Ÿš€ #14

In this article, we will explore the concept of enums in Solidity and demonstrate how they can be used effectively in smart contract development. Letโ€™s dive in! ๐ŸŠโ€โ™‚๏ธ

๐ŸŽฏ Understanding Enums in Solidity

Enums in Solidity are user-defined types that represent a set of named values. They provide a way to define a limited number of options or states that a variable can take. ๐ŸŒˆ Each option is assigned an implicit ordinal value, starting from zero for the first option and incrementing by one for each subsequent option. Enums are useful when you want to represent a finite set of choices or track the state of an object with a limited number of possible values.

๐Ÿ“ Declaring Enums in Solidity

To declare an enum in Solidity, you use the `enum` keyword followed by the name of the enum and a list of its possible values enclosed in curly braces. Letโ€™s take a look at an example: ๐Ÿ–‹๏ธ

```solidity
enum Status {
Pending,
Shipped,
Accepted,
Rejected,
Canceled
}
```

In this example, we have defined an enum called `Status` with five possible values: `Pending`, `Shipped`, `Accepted`, `Rejected`, and `Canceled`. ๐Ÿ“‹ By default, the first value listed in the enum definition (`Pending` in this case) is assigned the ordinal value of zero. Subsequent values are assigned ordinal values in increasing order.

๐Ÿงฉ Using Enums in Solidity Contracts

Enums can be used as data types for variables and function parameters in Solidity contracts. Letโ€™s create a contract called `Enum` to demonstrate how enums can be utilized: ๐Ÿ—๏ธ

contract Enum {
enum Status {
Pending,
Shipped,
Accepted,
Rejected,
Canceled
}
Status public status;
function get() public view returns (Status) {
return status;
}
function set(Status _status) public {
status = _status;
}
function cancel() public {
status = Status.Canceled;
}
function reset() public {
delete status;
}
}

๐Ÿ” Analyzing the Enum Contract

Now, letโ€™s analyze and discuss the different parts of the `Enum` contract and how enums are utilized:

1๏ธโƒฃ Enum Declaration:
The `Status` enum represents the shipping status of an item and has five possible values: `Pending`, `Shipped`, `Accepted`, `Rejected`, and `Canceled`. ๐Ÿ“ฆ

2๏ธโƒฃ Status Variable:
The `status` variable of type `Status` is declared as public, allowing external access. This variable holds the current shipping status of an item. ๐Ÿ“ฎ

3๏ธโƒฃ Getter Function (get):
The `get` function is a public view function that returns the current value of the `status` variable. It allows external entities to read the shipping status without modifying it. ๐Ÿ”

4๏ธโƒฃ Setter Function (set):
The `set` function is a public function that takes a `Status` parameter `_status` and updates the value of the `status` variable to the provided value. This function allows external entities to update the shipping status. ๐Ÿ“

5๏ธโƒฃ Cancel Function:
The `cancel` function sets the `status` variable to the `Canceled` value of the `Status` enum. It demonstrates how to assign a specific enum value to a variable. ๐Ÿšซ

6๏ธโƒฃ Reset Function:
The `reset` function uses the `delete` keyword to reset the `status` variable to its default value, which is the first element (`Pending`) of the `Status` enum. ๐Ÿ”„

๐Ÿ”„ Importing Enums in Solidity Contracts

Enums can also be declared outside of a contract and imported into other contracts for reuse. Letโ€™s explore how this can be done:

In a file named `EnumDeclaration.sol`, we define the `Status` enum: ๐Ÿ“

```solidity
enum Status {
Pending,
Shipped,
Accepted,
Rejected,
Canceled
}
```

In another contract, we can import and utilize the `Status` enum as follows: ๐ŸŒ

```solidity
import โ€œ./EnumDeclaration.solโ€;

contract Enum {
Status public status;
}
```

๐Ÿ’ก Conclusion

Enums in Solidity provide a structured and efficient way to model choices and track state in smart contracts. By understanding and utilizing enums effectively, developers can write more readable and maintainable code. ๐Ÿš€

In this article, we covered the basics of enums in Solidity, including declaration, usage in contracts, importing enums from external files, and analyzed a sample contract that demonstrates the practical use of enums. Enums are an essential tool in the Solidity developerโ€™s toolbox, enabling better code organization and state tracking in smart contract development.

๐Ÿ”— Additional Resources:

๐Ÿ”ฅ Now that you have a good grasp of enums in Solidity, you can leverage this powerful feature to enhance the functionality and structure of your smart contracts. Happy coding! ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป

--

--

Solidity Academy

Your go-to resource for mastering Solidity programming. Learn smart contract development and blockchain integration in depth. https://heylink.me/solidity/