Solidity -041 GeneralStructure

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

// Contract GeneralStructure demonstrates various Solidity features including state variables,
// structs, modifiers, events, and enums.
contract GeneralStructure {
    // State variables are stored on the blockchain and represent the contract's storage.
    int public stateIntVariable; // Public integer variable, automatically creates a getter function.
    string stateStringVariable; // Private string variable, not accessible outside the contract.
    address personIdentifier; // Address variable, typically used to store Ethereum addresses.
    MyStruct human; // Custom struct variable, used to represent complex data structures.
    bool constant hasIncome = true; // Constant boolean variable, its value is set at compile time and cannot be changed.

    // Struct definition for MyStruct.
    // Structs allow the creation of custom types to represent grouped data.
    struct MyStruct {
        string name; // Name of the person.
        uint myAge; // Age of the person.
        bool isMarried; // Marital status of the person.
        uint[] bankAccountsNumbers; // List of bank account numbers.
    }

    // Modifier onlyBy.
    // Modifiers are used to change the behavior of functions in a declarative way.
    modifier onlyBy() {
        require(msg.sender == personIdentifier, "Caller is not authorized");
        _; // Continues execution of the modified function.
    }

    // Event declaration for ageRead.
    // Events allow logging to the Ethereum blockchain. Useful for notifying clients about contract activities.
    event ageRead(address indexed _personIdentifier, int age);

    // Enumeration declaration for gender.
    // Enums are used to create custom types with a limited set of 'constant values'.
    enum gender { male, female }

    // Function getAge.
    // Demonstrates use of a modifier, struct instantiation, enum usage, and event emission.
    function getAge(address _personIdentifier) external onlyBy() payable returns (uint) {
        // Instantiating struct with initial values.
        human = MyStruct("Ritesh", 10, true, new uint[](3));
        
        // Using enum to set a gender.
        gender _gender = gender.male;
        
        // Emitting an event with the personIdentifier and stateIntVariable.
        emit ageRead(personIdentifier, stateIntVariable);
        
        // Function must return a value, example provided below is arbitrary and should be replaced with actual logic.
        return human.myAge;
    }
}
 

//Deploy:

  • 25
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值