1

I'm new in solidity programming so for learning purpose I'm developing e-voting system using ethereum blockchain. Code mentioned below,

pragma solidity 0.4.25;

contract RegisterCandidate {
    // Address Structure
    struct Address {
        string addressLine;
        string district;
        string state;
        uint pincode;     
    }

    // Candidate Structure
    struct Candidate {
        uint id;
        string name;
        Address addr;
        uint voteCount;
        uint age;
    }
    // Candidate mapping with candidate id
    mapping(uint => Candidate) public candidates;
    mapping(uint => Address) public addresses;
    // Store Candidates Count
    uint public candidatesCount;

    function addCandidate (string _name,uint _age,string _address,string _district,string _state,uint _pincode) public {
        candidatesCount ++;
        addresses[candidatesCount] = Address(_address,_district,_state,_pincode);
        candidates[candidatesCount] = Candidate(candidatesCount, _name,addresses[candidatesCount], 0, _age);
    }

}

I'm adding a candidate by using these commands,

RegisterCandidate.deployed().then(function(instance){app = instance})
app.addCandidate("Name",22,"Street","District","State",123456)

and to read the candidate,

app.candidates(1)

but whenever I try to read the candidate it is showing "Overflow" error. As here solidity provides build in geter function for public data members I'am unable to fetch the data.

On other hand I'm able to read address struct from addresses mapping

please help me to solve this issue

1

There are several issues but I think this is just for experimental use.

You've set up a 1:1 relationship between candidates and addresses and you're storing the address in a separate mapping, so also storing in the candidate is a redundancy.

You can "turn off" the "free" getter and make your own. In the case of the nested struct, it's not working. In any case, it wasn't a good direction.

This is working:

pragma solidity 0.4.25;

contract RegisterCandidate {
    // Address Structure
    struct Address {
        string addressLine;
        string district;
        string state;
        uint pincode;     
    }

    // Candidate Structure
    struct Candidate {
        uint id;
        string name;
        uint voteCount;
        uint age;
    }
    // Candidate mapping with candidate id
    mapping(uint => Candidate) candidates;  // don't want a free getter
    mapping(uint => Address) public addresses;
    // Store Candidates Count
    uint public candidatesCount;

    function getCandidate(uint key) public view returns(string name, string addressLine, string district, string state, uint pincode, uint voteCount, uint age) {
        Address storage a = addresses[key];
        Candidate storage c = candidates[key];
        return (c.name, a.addressLine, a.district, a.state, a.pincode, c.voteCount, c.age);
    }

    function addCandidate (string _name,uint _age,string _address,string _district,string _state,uint _pincode) public {
        candidatesCount ++;
        addresses[candidatesCount] = Address(_address,_district,_state,_pincode);
        candidates[candidatesCount] = Candidate(candidatesCount, _name, 0, _age);
    }

}

I would question the storage structure if this was a real project.

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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