envoy1084
/
30-Days-of-Solidity
30 Days of Solidity step-by-step guide to learn Smart Contract Development.
WARNING: This repository is currently undergoing updates and revisions to incorporate the latest information and advancements in Solidity programming. Please be advised that the content may not be up-to-date or accurate during this time. We expect the updates to be completed within the next 30 days, and appreciate your patience during this process. Thank you for your understanding.
Contents
- Day 1 - Licenses and Pragma
- Day 2 - Comments
- Day 3 - Initializing Basic Contract
- Day 4 - Variables and Scopes
- Day 5 - Operators
- Day 6 - Types
- Day 7 - Functions
- Day 8 - Loops
- Day 9 - Decision Making
- Day 10 - Arrays
- Day 11 - Array Operations
- Day 12 - Enums
- Day 13 - Structs
- Day 14 - Mappings
- Day 15 - Units
- Day 16 - Require Statement
- Day 17 - Assert Statement
- Day 18 - Revert Statement
- Day 19 - Function Modifiers
- Day 20…
This is Day 13 of 30 in Solidity Series
Today I Learned About Structs in Solidity.
Structs in Solidity allows you to create more complicated data types that have multiple properties. You can define your own type by creating a struct.
They are useful for grouping together related data.
Structs can be declared outside of a contract and imported in another contract. 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;
}
Example:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
contract structType {
struct Student {
string name;
string gender;
uint roll_no;
}
// Declaring a structure object
Student student1;
// Assigning values to the fields for the structure object student2
Student student2 = Student("Jeff","Male",1);
// Defining a function to set values for the fields for structure
// student1
function setStudentDetails() public {
student1 = Student("Teresa","Female",2);
}
// Defining a function to get Student Details
function studentInfo() public view returns (string memory, string memory, uint) {
return(student1.name , student1.gender , student1.roll_no);
}
}
Output -
When we run setStudentDetails it stores values in the structure student2
and when we call the studentInfo function we get
{
"0": "string: Teresa",
"1": "string: Female",
"2": "uint256: 2"
}
Top comments (0)