Git Product home page Git Product logo

Comments (2)

DavidKnott avatar DavidKnott commented on June 12, 2024

@Katsu1991 In short you can't return a struct but you can return multiple arrays (one for each variable in the struct).
This is an example of the code you'll need to use:


contract People {

  Person[] public people;

  struct Person {
    bytes32 firstName;
    bytes32 lastName;
    uint    age;
  }

  function addPerson(bytes32 _firstName, bytes32 _lastName, uint _age) returns (bool success) {
    Person memory newPerson;
    newPerson.firstName = _firstName;
    newPerson.lastName = _lastName;
    newPerson.age = _age;

    people.push(newPerson);
  }

  function getPeople() constant returns (bytes32[], bytes32[], uint[]){

    uint length = people.length;

    bytes32[] memory firstNames = new bytes32[](length);
    bytes32[] memory lastNames = new bytes32[](length);
    uint[] memory ages = new uint[](length);
  
    for (uint i = 0; i < people.length; i++){
      Person memory currentPerson;
      currentPerson = people[i];
      firstNames[i] = currentPerson.firstName;
      lastNames[i] = currentPerson.lastName;
      ages[i] = currentPerson.age;

    }
  return (firstNames, lastNames, ages);
  }

}

Here is a link to an in depth explanation of the above code:
https://www.youtube.com/watch?v=3-XPBtAfcqo&t=2115s.

from solidity-baby-steps.

Katsu1991 avatar Katsu1991 commented on June 12, 2024

@DavidKnott thanks ,I got it.

from solidity-baby-steps.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.