Skip to content

Instantly share code, notes, and snippets.

@minhth1905
Last active May 13, 2024 11:06
Show Gist options
  • Select an option

  • Save minhth1905/4b6208372fc5e7343b5ce1fb6d42c942 to your computer and use it in GitHub Desktop.

Select an option

Save minhth1905/4b6208372fc5e7343b5ce1fb6d42c942 to your computer and use it in GitHub Desktop.

Revisions

  1. minhth1905 revised this gist Oct 12, 2019. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions return array in solidity
    Original file line number Diff line number Diff line change
    @@ -19,6 +19,12 @@ contract Money {
    peoples[candidateConut] = People(candidateConut,_name,0);
    candidateConut++;
    }

    function addManyCandidate(string[] memory names) public {
    for(uint i = 0; i < names.length; i++) {
    addCandidate(names[i]);
    }
    }
    //return Single structure
    function get(uint _candidateId) public view returns(People memory) {
    return peoples[_candidateId];
  2. minhth1905 created this gist Oct 12, 2019.
    50 changes: 50 additions & 0 deletions return array in solidity
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    pragma solidity ^0.5.0;
    pragma experimental ABIEncoderV2;
    contract Money {
    struct People{
    uint id;
    string name;
    uint amount;
    }
    mapping (uint => People) public peoples;
    event votedEvent(uint indexed _candidateId);
    uint public candidateConut;

    constructor() public {
    candidateConut = 0;
    addCandidate("Holder 1");
    addCandidate("Holder 2");
    }
    function addCandidate(string memory _name) public {
    peoples[candidateConut] = People(candidateConut,_name,0);
    candidateConut++;
    }
    //return Single structure
    function get(uint _candidateId) public view returns(People memory) {
    return peoples[_candidateId];
    }
    //return Array of structure Value
    function getPeople() public view returns (uint[] memory, string[] memory,uint[] memory){
    uint[] memory id = new uint[](candidateConut);
    string[] memory name = new string[](candidateConut);
    uint[] memory amount = new uint[](candidateConut);
    for (uint i = 0; i < candidateConut; i++) {
    People storage people = peoples[i];
    id[i] = people.id;
    name[i] = people.name;
    amount[i] = people.amount;
    }

    return (id, name,amount);

    }
    //return Array of structure
    function getPeoples() public view returns (People[] memory){
    People[] memory id = new People[](candidateConut);
    for (uint i = 0; i < candidateConut; i++) {
    People storage people = peoples[i];
    id[i] = people;
    }
    return id;
    }
    }