structs are used to represent the object or classes in java, interface, and classes in typescript.

Structs used to combine the

Suppose you want to store employee data in an Ethereum block and the employee contains id, name, and salary details. For example, In contracts, declare three variables as seen below

   uint id;
   string name;
   uint salary;

if you want to save this data to contract, write a function, pass three arguments to the function and save the data, again data is returned from the function with three variables.

The problem with approaching two things

  • if employee data wants to send debt, you have to modify it in all places with the fourth variable. This makes coding very difficult if more number variables are added.
  • Tedious task to type variables and pass the data and code functions with the same type.
  • The code looks not simple

structs solve those problems by grouping multiple types into a single name.

It acts as a new data type and can be declared, accessed, passed as a parameter, and return as a value.

How to declare structs in solidity?

struct is the keyword used to define a new custom type to group multiple type fields.

Following is an example of a defined struct in solidity

 struct structname {
      type variable1;
      type variable2;
      type variable3;

      type variablen;
}

struct: is a keyword in solidity to declare struct type structname: It is a new type similar to reference types and it is a valid identifier in solidity. fields in solidity: each contains type and variable names to hold the data.

Once the struct structure is defined, you can declare a variable of this struct like a normal variable declaration.

Here is a syntax to declare a struct variable

structname variablename;

How to Initialize data a Struct in a solidity?

let’s define a new struct struct

struct Employee {
      uint id;
      string name;
      uint salary;
   }

Declare a variable of Employee

   Employee employee;

There are many ways we can initialize struct fields with data

one way, using a constructor with declared ordered field values

 employee = Employee(1,'john', 5000);
 employee = Employee(1,'john');

if data order types changed like below, throw compilation error TypeError: Invalid type for argument in the function call. Invalid implicit conversion from literal_string “john” to uint256 requested.

 employee = Employee(1,5000,'john'); //  throws error

Another way is using a named field wrapped inside parenthesis ({})syntax

 employee = Employee({id:2, salary:500,name:"john"});

Another way, using assigned each field with data

Employee employee = Employee();
 employee.id=4;
 employee.name="john";
 employee.salary=4000;

Data is added to a struct in the global scope or inside a constructor or function.

How to access struct data inside a solidity function

struct data fields can be accessed using dot (.) notation or square brackets([]) syntax.

 employee["id"];
 employee.id;

Here is an example function to get an employee id from a struct

   function getEmployyeeId() public view returns (uint) {
      return employee.id;
   }

Struct a simple example

This demonstrates the simple struct example.

Here is an struct simple example

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.7;

contract test {
   struct Employee {
      uint id;
      string name;
      uint salary;
   }
   Employee employee;

   function addEmployee() public {
      employee = Employee(1,'john', 5000);
   }
   function getEmployyeeId() public view returns (uint) {
      return employee.id;
   }
}