Solidity – Enums and Structs

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.

Syntax:

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;  
    }  
}

chevron_right


Output : 



Enums

Struct

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. 

Syntax:

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.name, book2.writter, 
               book2.id, book2.available);  
    
     
   // Defining function to print 
   // book1 details
   function get_details(
   ) public view returns (string memory, uint) {
      return (book1.name, book1.id);
   }
}

chevron_right


Output : 
 

Structs




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.