Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Solidity – Enums and Structs

  • Last Updated : 13 Jul, 2020

Enums are the way of creating user-defined data types, it is usually used to provide names for integral constants which makes the contract better for maintenance and reading. Enums restrict the variable with one of few predefined values, these values of the enumerated list are called as enums. Options of are represented with integer values starting from zero, a default value can also be given for the enum. By using enums it is possible to reduce the bugs in the code.


enum <enumerator_name> { 
            element 1, elemenent 2,....,element n

Example: In the below example, the contract Types consist of an enumerator week_days, and functions are defined to set and get the value of a variable of type enumerator.


// Solidity program to demonstrate 
// how to use 'enumerator'
pragma solidity ^0.5.0; 
// Creating a contract
contract Types {  
    // Creating an enumerator
    enum week_days 
    // 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;  

Output : 



Solidity allows user to create their own data type in the form of structure. The struct contains a group of elements with a different data type. Generally, it is used to represent a record. To define a structure struct keyword is used, which creates a new data type. 


struct <structure_name> {  
   <data type> variable_1;  
   <data type> variable_2; 

For accessing any element of the structure dot operator is used, which separates the struct variable and the element we wish to access. To define the variable of structure data type structure name is used.

Example: In the below example, the contract Test consists of a structure Book, and functions are defined to set and get values of the elements of the structure.


// Solidity program to demonstrate 
// how to use 'structures'
pragma solidity ^0.5.0;
// Creating a contract
contract test {
   // Declaring a structure
   struct Book { 
      string name;
      string writter;
      uint id;
      bool available;
   // Declaring a structure object
   Book book1;
   // Assigning values to the fields 
   // for the structure object book2
   Book book2 
     = Book("Building Ethereum DApps"
            "Roberto Infante "
             2, false);
   // Defining a function to set values 
   // for the fields for structure book1
   function set_book_detail() public {
      book1 = Book("Introducing Ethereum and Solidity"
                   "Chris Dannen"
                    1, true);
   // Defining function to print 
   // book2 details
   function book_info(
   )public view returns (
     string memory, string memory, uint, bool) {  
        return(, book2.writter, 
     , book2.available);  
   // Defining function to print 
   // book1 details
   function get_details(
   ) public view returns (string memory, uint) {
      return (,;

Output : 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!