1

Can someone help me with a nested struct, I am unable to map the nested struct. I need to get into a struct some user details like name, gender, etc., and the user's home address as well, I tried the following but did not get anywhere:

struct Pdetails {
    
    string fName;
    string lName
    int256 age;
        enum gender;
    }
struct Addr{
    
    string line 1;
    string line 2;
    string city;
    string state;
    int256 pincode;
    }


function createUser(address _userAddress, uint256 _userId, string _fName, string _lName, int256 _age, enum _gender, string _line1, string _line2, string _city, string _state, int256 _pincode  ) public {
    User storage user = users[_userAddress];
    require(!user.set);
    
    //Store the user
    users[_userAddress] = User(
        {
        id: _userId,
        first_name: _fName,
        last_name: _lName,
        age: _age,
        gender : _gender,
        Addr line1: _line1,
        Addr line2: _line2,
        city: _city,
        state: _state,
        pincode: _pincode
        
        set: true
    }
    );
}
i tried to map these but i dont think this is the right way to do it

2 Answers 2

1

You can combine both of the structs into a new struct to store the information of the user. for example:

pragma solidity 0.8.2;

contract PersonalInfo{
    
    enum gender {MALE, FEMALE}
    
    struct Pdetails {
    
        string fName;
        string lName;
        int256 age;
        gender Gender;
    }
    
    struct Addr{
    
        string line;
        string city;
        string state;
        int256 pincode;
    }
  
    struct UserInfo{
        
        Addr addr;
        Pdetails pdetails;
    }
}
1
  • can u help me map these as well? i am trying to get user info of above parameters, i posted my code in answer Apr 18, 2021 at 0:57
0
  1. As it was mentioned in the first answer, It is easier if you combine your structs into one:
enum Gender{MALE, FEMALE}
    // Returns uint
    // MALE  - 0
    // FEMALE - 1
    
    struct Pdetails {
        string fName;
        string lName;
        int256 age;
        Gender gender;
    }
    
    struct Addr{
        string line;
        string city;
        string state;
        int256 pincode;
    }
  
    struct UserInfo{              
        Pdetails pdetails;
        Addr addr;
    }
  1. Now regarding the mapping, you will have to have an elementary type (int, string, etc...) to be used as key, the value however can have any type:
mapping(key => value) mapName

In this case, I added another global variable called id, and by doing so, you can look for a person by his/her id. Your new combined struct will be this:

struct UserInfo{              
        Pdetails pdetails;
        Addr addr;
        uint id;
    }

Your map will be this:

mapping(uint => UserInfo) idToUserInfo

Now sum it all up:

pragma solidity ^0.8.10;

contract Struct {

    enum Gender{MALE, FEMALE}
    // Returns uint
    // MALE  - 0
    // FEMALE - 1
    
    struct Pdetails {
        string fName;
        string lName;
        int256 age;
        Gender gender;
    }
    
    struct Addr{
        string line;
        string city;
        string state;
        int256 pincode;
    }
  
    struct UserInfo{              
        Pdetails pdetails;
        Addr addr;
        uint id;
    }

    mapping(uint => UserInfo) idToUserInfo;

    function get(uint _id) public view returns (UserInfo memory) {
        return idToUserInfo[_id];
    }

    function set(Pdetails memory _pDetails, Addr memory _addr, uint _id) public {
        idToUserInfo[_id] = UserInfo(_pDetails, _addr, _id);
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.