Enum in solidity from zero to hero

By akohad Oct20,2022

[ad_1]

  • Enums are one way to create user-defined type in solidity
  • Enums are value type comprising a pre-defined list of constant values
  • constant values with an enum can be explicity converted to integer
  • Each constant values gets an integer value start from zero
  • Enums require at least one number
  • Enums cannot have more than 256 members
  • You can’t use numbers (positive or negative) or boolean (true or false in lowercase) as members for an enum. However, True and False (Capitalised) are accepted.
  • You do not need to end the enum declaration with a semicolon. The compiler uses the semicolon ; and curly brackets {} to determine the scope of the code
  • Using type(name of enum).min get the smallest value
  • Using type(name of enum).max get the largest value
enum status {ON,OFF}
status lightOn=status.ON;
status lightOff=status.OFF
enum example in solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.16;
contract enumTest{
enum shipMent{notFound,approved,rejected}
shipMent status;
shipMent defaultStatus=shipMent.notFound;
function setToApproved() public{
status=shipMent.approved;
}
function setToRejected() public{
status=shipMent.rejected;
}
function getChoise() public view returns(shipMent){
return status;
}
function getDefaultStatus() public view returns(shipMent){
return defaultStatus;
}
function getSmallestValue() public pure returns(shipMent){
return type(shipMent).min;
}
function getLargestValue() public pure returns(shipMent){
return type(shipMent).max;
}
}
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.8;

contract test {
enum ActionChoices { GoLeft, GoRight, GoStraight, SitStill }
ActionChoices choice;
ActionChoices constant defaultChoice = ActionChoices.GoStraight;

function setGoStraight() public {
choice = ActionChoices.GoStraight;
}

function getChoice() public view returns (ActionChoices) {
return choice;
}

function getDefaultChoice() public pure returns (uint) {
return uint(defaultChoice);
}

function getLargestValue() public pure returns (ActionChoices) {
return type(ActionChoices).max;
}

function getSmallestValue() public pure returns (ActionChoices) {
return type(ActionChoices).min;
}
}

// Solidity program to demonstrate
// how to use 'enumerator'
pragma solidity ^0.5.0;
// Creating a contract
contract Types {
// Creating an enumerator
enum week_days
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
// Declaring variables of
// type enumerator
week_days week;

week_days choice;

// Setting a default value
week_days constant default_value
= week_days.Sunday;

// Defining a function to
// set value of choice
function set_value() public {
choice = week_days.Thursday;
}

// Defining a function to
// return value of choice
function get_choice(
) public view returns (week_days) {
return choice;
}

// Defining function to
// return default value
function getdefaultvalue(
) public pure returns(week_days) {
return default_value;
}
}

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
contract test {// predefined value enum EnumName
enum FreshJuiceSize{ SMALL, MEDIUM, LARGE }
//Enum variable
FreshJuiceSize choice;
// access default choice
FreshJuiceSize constant defaultChoice = FreshJuiceSize.MEDIUM;
function setLarge() public {
choice = FreshJuiceSize.LARGE;
}
function getChoice() public view returns (FreshJuiceSize) {
return choice;
}
function getDefaultChoice() public pure returns (uint) {
return uint(defaultChoice);
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Enum {
//enum representing shipping status
enum Status {Pending,Shipped, Accepted, Rejected,Canceled}
//enum variable, Default value pending =0
Status public status;
function getChoice() public view returns (Status) {
return status;
}
// Update status by passing uint into input
function set(Status _status) public {
status = _status;
}
//You can update to a specific enum like this
function cancel() public {
status = Status.Canceled;
}
// delete resets the enum to it's first value, 0
function reset() public {
delete status;
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Enum {
enum Status {Pending,Shipped, Accepted, Rejected,Canceled}
Status public status;
function nextChoice() public {
status=Status(uint(status)+1);
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract EnumTest {
enum Status {Pending,Shipped, Accepted, Rejected,Canceled}
function explicitConvertion() public pure returns(uint){
return uint(Status.Accepted);
}
}

Example from stack exchange

// Lets see Enums which are often used for state machine like this
enum State { Created, Locked, Inactive };

// post this a variable can be Declared like this
State public state;

// Initializing the state can be done like this
state = State.Created;

// It is important to note that enums can be explicitly converted to ints like this
uint createdState = uint(State.Locked);

Enum & mapping

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Enum {
enum Status {Pending,Shipped, Accepted, Rejected,Canceled}
mapping(uint=> Status) enumMappnig;
}

Enum & struct

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Enum {
enum Status {Pending,Shipped, Accepted, Rejected,Canceled}struct enumStruct{
Status _status;
}
}

New to trading? Try crypto trading bots or copy trading

[ad_2]

Source link

By akohad

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *