pragma solidity ^0.5.0;

contract Election {
    //Model a Candidate
    struct Candidate {
        uint id;
        string name;
        uint voteCount;

    //Store Candidate
    //Fetch Candidate
    mapping(uint => Candidate) public candidates;
    // Store Candidates Count
    uint public candidatesCount;

    function Elections () public {
        addCandidate("Candidate 1");
        addCandidate("Candidate 2");

    function addCandidate (string memory _name) private {
        candidatesCount ++;
        candidates[candidatesCount] = Candidate(candidatesCount, _name, 0);


You are using Solidity version 0.5.0

If your contract name is "Elections", it must now be changed to include the constructor call explicitly. See the "breaking" changes in Solidity 0.5.0 here!.

Change the constructor call from function Elections () public to constructor () public

For this program execution:

  1. After the above changes, execute your contract once again on your local network using truffle migrate --reset

  2. Now get back to your truffle console and use Election.deployed().then(function (i) { app = i }) followed by

  3. app.candidates or app.candidates[0]
| improve this answer | |

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.