// SPDX-License-Identifier: MITpragmasolidity ^0.8.0;contract Enum {// Enum representing shipping statusenumStatus { Pending, Shipped, Accepted, Rejected, Canceled }// Default value is the first element listed in// definition of the type, in this case "Pending" Status public status;// Returns uint// Pending - 0// Shipped - 1// Accepted - 2// Rejected - 3// Canceled - 4functionget() publicviewreturns (Status) {return status; }// Update status by passing uint into inputfunctionset(Status_status) public { status = _status; }// You can update to a specific enum like thisfunctioncancel() public { status = Status.Canceled; }// delete resets the enum to its first value, 0functionreset() public {delete status; }}
Declaring and Importing Enum
File that the enum is declared in.
// SPDX-License-Identifier: MITpragmasolidity ^0.8.0;// This is saved 'EnumDeclaration.sol'enumStatus { Pending, Shipped, Accepted, Rejected, Canceled}
File that imports the enum above.
// SPDX-License-Identifier: MITpragmasolidity ^0.8.0;import"./EnumDeclaration.sol";contract Enum { Status public status;}