1

I have the below code

struct Ticket {
        uint256 id;
        uint256[] numbers;
        address payable owner;
}


function enter(uint256[] memory userNumbers) public {
        Ticket memory newTicket = Ticket(
            id,
            userNumbers,
            payable(msg.sender)
        );

        entries[id][msg.sender] = newTicket;
    }

However, when I inspect the Ticket object saved, I only get the id, and address.

How should I properly save the userNumbers inside the struct?

1 Answer 1

0

This works fine for me. But it does not show the dynamic array in Remix if that is what you mean? However, this does not mean that the array is not there. It just seems like a UI bug/limitation.

I wrote this code to test it:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract StackOverflowAnswer {

  struct Ticket {
        uint id;
        uint[] numbers;
        address payable owner;
  }
  mapping(uint => mapping(address => Ticket)) public entries;

  function enter(uint256[] memory userNumbers, uint id) public {

    Ticket memory newTicket = Ticket(
        id,
        userNumbers,
        payable(msg.sender)
    );
    
    entries[id][msg.sender] = newTicket;
  }

  function seeTicket(uint id, address _address) public view returns (uint[] memory) {
    return entries[id][_address].numbers;
  }
}

I wrote an additional method just to retrieve the dynamic array part of the struct, and it renders it when specifically selected, I guess:

enter image description here

2
  • yes, that's what I meant. Remix does not show/display the array. and ....hoolllyyyy........ you are right! Would you know why this is? I suddenly don't feel comfortable with how I wrote it now since it gets buggy on remix...
    – momoja
    Commented Jul 20, 2022 at 7:35
  • I am not sure why they won't show it. Still, I know that in the past, they had even more significant limitations (you couldn't create a list of lists at all, not because of solidity or javascript by themselves, but due to the translation between them, at least now you can, but it just won't display correctly I suppose). You could also try developing outside of Remix like in Hardhat that should work, and then use test-driven development to ensure that everything is built the way you want it :-) Commented Jul 20, 2022 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.