Solidity Steps
  • Learning Solidity
  • Step 1
    • 1: Introduction
    • 2: Data Types
    • 3: Functions
    • 4: Control Structures
    • 5: State Variable
    • 6: Local Variables
    • 7: Global Variables
    • 8: View Keyword
    • 9: Pure Keyword
  • STEP 2
    • 10: Immunable Keyword
    • 11: Events
    • 12: Condition
    • 13: While Loop
    • 14: Do While Loop
    • 15: For Loop
    • 16: Required
    • 17: Assert
    • 18: Revert
    • 19: Modifier
  • STEP 3
    • 20: Constructor
    • 21: Mapping
    • 22: Array
    • 23: Enum
    • 24: Structs
    • 25: Data Location
    • 26: Inheritance
    • 27: The Shadowing Effect
    • 28: Super Keyword
    • 29: Visibility
  • STEP 4
    • 30: Interface
    • 31: Abstract Contract
    • 32: Payable
    • 33: Using type()
    • 34: Sending Ether
    • 35: Receive
    • 36: Fallback
    • 37: Call
    • 38: DelegateCall
    • 39: Calling Other Contracts
  • STEP 5
    • 40: Factory Contract
    • 41: Proxy Contract
    • 42: Create2
    • 43: Try and Catch
    • 44: Solidity Library
    • 45: ABI Encoded
    • 46: ABI Decoded
    • 47: Keccak256
    • 48: Function Signature Hash
  • TIPS
    • Tips: Solidity by "Immunable"
    • Tips: Truffle Tutorial
    • Tips: Microblog Dapp
    • Tips: Reentrancy
    • Tips: Slither Tutorial
    • Tips: Remix Tutorial
    • Tips: Hardhat Tutorial
  • CAREER
    • 💲Cover Letter
    • 💲Resume
  • ABOUT
    • Contact me
Powered by GitBook
On this page
  • Structs
  • Import Structs
  • Conclusion
  1. STEP 3

24: Structs

Structs

Structs are custom data types that allow developers to define the data that they would like to store. They are typically used to store data that is related to one another, such as a user profile, a product, or a transaction record. Structs provide a convenient way to organize and access related information.

  • User Profile

struct Profile {    
    string name;    
    string email;    
    uint age;    
    string address;
}

The above code defines a Profile struct which contains 4 fields: name, email, age, and address.

  • Product

struct Product {    
    string name;    
    uint price;    
    uint quantity;    
    string description;
}

The above code defines a Product struct which contains 4 fields: name, price, quantity, and description.

  • Transaction Record

struct TransactionRecord {    
    address sender;    
    address recipient;    
    uint amount;    
    uint timestamp;
}

The above code defines a TransactionRecord struct which contains 4 fields: sender, recipient, amount, and timestamp.


Import Structs

Structs are a powerful tool in Solidity, allowing you to create complex data structures. In this article, we'll look at how to use Import Structs to create better, more maintainable smart contract code.

  • Why Use Import Structs? Importing Structs can help make your smart contracts more maintainable and reusable. By importing Structs, you can define a structure once and then use it in multiple contracts. This helps you keep your code DRY (Don't Repeat Yourself).

  • How to Use Import Structs Using Import Structs is quite straightforward. All you need to do is define a Struct in a separate file and then import it into your smart contract.

Let's look at an example.

  • Example Let's say you have a smart contract for an auction. In the auction smart contract, you want to store the details of each bidder. This will include their name, address, and bid amount.

To do this, you can define a Struct in a separate file like this:

contract Bid { 
    struct Bidder {    
        string name;    
        address addr;    
        uint256 bidAmount;
    }
}

Then, in the auction smart contract, you can import the Struct and use it to store the bidder details like this:

import "./Bid.sol"; 
contract Auction {    
    Bidder[] bidders; 
    function addBidder(string memory _name, address _addr, uint256 _bidAmount) public {        
        bidders.push(Bidder(_name, _addr, _bidAmount));    
    }
}

Conclusion

Import Structs can help you make your smart contracts more maintainable and reusable. By defining Structs in separate files and then importing them into your smart contracts, you can keep your code DRY and more organized.

That's it for the lesson 24! In the next lesson, Data Location

Previous23: EnumNext25: Data Location

Last updated 2 years ago