Search code examples
ethereumsolidity

Problem push values to an enum array in a struct


Im trying to push a value to an enum array stored in a struct but im not able to push a value. Where am I going wrong? This is my code:

// SPDX-License-Identifier: UNLICENSED

pragma solidity 0.8.12;

contract Test {
    address private owner; // owner is Insurance Company

    mapping (address => Record) records;
    enum Animal {Lion, Tiger, Zebra}

    struct Record {
        Animal[] animal; // status of the record
    }

    constructor() { // set owner on deploy as deployer address
        owner = msg.sender;
    }

    function addRecord() public {
        Record storage record = records[msg.sender];
        record.animal.push(0);
        // record.animal[0] = Lion;
        // record.animal = [Lion, Tiger};
    }
}

My error:

TypeError: Member "push" not found or not visible after argument-dependent lookup in enum Test.Animal[] storage ref.
--> b.sol:21:9:
|
21 | record.animal.push(0);
| ^^^^^^^^^^^^^^^^^^

Solution

  • Try this:

    record.animal.push(Animal.Lion);