Skip to content

Instantly share code, notes, and snippets.

@finxter
Forked from yassesh/structArray2.sol
Created December 28, 2021 08:28
Show Gist options
  • Save finxter/cd4ea22a85fa9d5c236a4e2d0fa996b9 to your computer and use it in GitHub Desktop.
Save finxter/cd4ea22a85fa9d5c236a4e2d0fa996b9 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.7+commit.e28d00a7.js&optimize=false&runs=200&gist=
pragma solidity ^0.8.0;
pragma experimental ABIEncoderV2;
contract SavingsAccount {
struct Member{
uint id;
string name;
uint balance;
}
mapping (uint => Member) public members;
event savingsEvent(uint indexed _memberId);
uint public memberCount;
constructor() public {
memberCount = 0;
addMember("chris",9000);
addMember("yassin",6000);
}
function addMember(string memory _name,uint _balance) public {
members[memberCount] = Member(memberCount,_name,_balance);
memberCount++;
}
//return Single structure
function get(uint _memberId) public view returns(Member memory) {
return members[_memberId];
}
//return Array of structure Value
function getMember() public view returns (uint[] memory, string[] memory,uint[] memory){
uint[] memory id = new uint[](memberCount);
string[] memory name = new string[](memberCount);
uint[] memory balance = new uint[](memberCount);
for (uint i = 0; i < memberCount; i++) {
Member storage member = members[i];
id[i] = member.id;
name[i] = member.name;
balance[i] = member.balance;
}
return (id, name,balance);
}
//return Array of structure
function getMembers() public view returns (Member[] memory){
Member[] memory id = new Member[](memberCount);
for (uint i = 0; i < memberCount; i++) {
Member storage member = members[i];
id[i] = member;
}
return id;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment