I'm developing a dapp on Meteor that uses a contract written in solidity. In the contract there is a struct. Is possible to print the values that are in the struct? How can i do this?

| improve this question | | | | |

If you have a single Struct that you want to get the value for, you can (1) simply make it a public variable or (2) create a getter in the contract, something like getStruct below:

struct MyStruct {
  uint a;
  uint b;
  uint c;

MyStruct public myStruct = MyStruct(1, 2, 3);

function getStruct()
  returns (uint a, uint b, uint c)
  return (myStruct.a, myStruct.b, myStruct.c);

For your DApp, if the Struct is public, you can simply just call that variable in web3, contractInstance.myStruct.call() which returns and array [a,b,c];

Or you can use the getter function (contractInstance.getStruct()) which would return an array [a, b, c].

| improve this answer | | | | |
  • What is the _address parameter? – Giulio Magnanini Oct 11 '17 at 10:44
  • @GiulioMagnanini I changed the response to just get the single MyStruct values. My previous response assumed you'd have multiple Structs indexed by an address (mapping (address => MyStruct) public myList), so you would retrieve a Struct relating to an input address (_ in _address simply denotes an input, just code styling) by something like myList[_address]. – carlolm Oct 11 '17 at 17:36

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.