Enum defines multiple values and creates the object with one of the values. Enum is an enumerable constant in Solidity.
The user creates a custom type to select one constant from multiple predefined constants.
Enums are useful to hold the status of transactions status and the flow of small contracts.
How to Create and declare an enum in solidity?
Enum
types can be declared using the enum
keyword.
It assigns the index to each constant in the enumeration list, For example, constants=0,constant=1 … constants=N-1.
Syntax:
enum enumName {constants1,constant2 ... constantsN}
Once the Enum type is declared, you can create a variable of a new type
enumName [visibile] variableName;
enumName is a new type declared with an enum visible are valid visible modifiers in solidity variableName is a valid variable name
Here is an example of below things
- How to create an enum custom type
- How to declare an enum variable and access value.
// Solidity program example for Enum Type declaration
pragma solidity ^0.5.0;
// Defining contract
contract EnumTest {
enum Number { One, Two, Three}
Number public number;
constructor(){
}
}
Let’s see some examples
How to access Enum values in solidity?
// Defining contract
contract EnumTest {
enum Number { One, Two, Three}
Number public number=Number.Three;
constructor() public {
}
function getNumber() public view returns (Number) {
return number;
}
Output: { “0”: “uint8: 2” }
How to Convert Enum to number type in solidity
Enum constants are defined with index=0 to order the enum constants.
By default, It does the implicit conversion of enum constants to the number, and explicit conversion is not allowed.
use uint(enumvariable)
to get uint
or integer value.
Here is an example code
// Solidity program example for Enum Type declaration
pragma solidity ^0.5.0;
// Defining contract
contract EnumTest {
enum Number { One, Two, Three}
Number public number=Number.Three;
constructor() public {
}
function getNumber() public view returns (uint) {
return uint(number);
}
}
Output:
{
"0": "uint256: 2"
}
How to Convert Enum to String in solidity?
Enum types are converted to strings using a custom function. This function returns a string for each enum constant.
// Solidity program example for Enum Type declaration
pragma solidity ^0.5.0;
// Defining contract
contract EnumTest {
enum Number { One, Two, Three}
Number public number=Number.Three;
constructor() public {
}
function getNumber() public view returns (uint) {
return uint(number);
}
function getNumberName() external view returns (string memory) {
Number strNumber = number;
if (strNumber == Number.One) return "One";
if (strNumber == Number.Two) return "Two";
if (strNumber == Number.Three) return "Three";
return "";
}
}
Output:
{
"0": "string: Three"
}
How to find Invalid enums syntax errors
Below are the following errors for invalid enum
values
Enum values do not contain Boolean values(true, false), But strings True and False are accepted.
enum Number { true, Two, Three}
- Numbers are not allowed, for example, negative and positive numbers
enum Number { 1, Two, Three}
- Semicolons are not ended for enum declaration
The below declaration gives an error
ParserError: Function, variable, struct or modifier declaration expected.
enum Number { One, Two, Three};
Removing semicolons (;) from the line end works and compiling the file.
How to declare a variable of a string?
Variables declared of a string with the below syntax.
string [scope] name;
scope
is an optional such as public, private, and valid solidity scopes.
string
is a keyword and data type defined in the language.
name
is an identifier declared and valid names are alphanumeric which starting must be a letter. Reserved words not used with name value.
Another way using `string literal syntax allows you to declare and assign the values with a single statement.
string public name = "john";
String literals are enclosed in double quotes(") or single quotes(')
If it is declared in a global contract, called a global scope variable
pragma solidity ^0.5.0;
// Create contract for String type testing
contract StringTest {
string name;
string role="admin";
}
There is another way to declare string types with byte32. Byte32 works with string declaration as well as string literal syntax.
pragma solidity ^0.5.0;
// Create contract for String type testing
contract StringTest {
byte32 name;
byte32 role="admin";
}
Both string
and byte32
do store the string data.
String takes more gas compared to byte32. It is always preferable to use byte32 in place of string.
Explicit conversion is not required to convert byte32 to/from a string.