Created
June 10, 2022 22:35
-
-
Save helderjnpinto/28ea754f1cbd2c6b3857ea4c1d9a4186 to your computer and use it in GitHub Desktop.
Working with views dinamic arrays in solidity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.8.14; | |
// SPDX-License-Identifier: GPL-3.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() { | |
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment