1

I'm new to solidity programming and experimenting with what I've learned from the cryptozombies.io. I'm unable to copy data from the globalFarmers variable to my local function's variable itemSpecificFarmers.

Here is what I'm trying to do. A manufacturer should be able to add specific farmers' data, that a manufacturer used in making an item. I'm using the remix compiler.

I have found many questions here that were posted before and I tried to replicate the solution but had no luck, also the information is too overwhelming that I can't comprehend what exactly is wrong that I'm doing.

This is my main contract.

pragma solidity >=0.5.0 <0.6.0;
import "./farm.sol";

contract Test {
    struct Item {
        Farmer[] farmers;
        string manufacturerName;
        string manufacturerDate;
        string distributorDataDate;
        string distributorName;
    }

    Farmer[] public globalFarmers;
    Item public Item;

    function addFarmData(string memory _name, string memory _item) public {
        globalFarmers.push(Farmer(_name, _item));
    }

    function addManufacturerData(
        uint256[] memory _farmerIds,
        string memory _name,
        string memory _date
    ) public {
        Farmer[] memory itemSpecificFarmers = new Farmer[](_farmerIds.length - 1);
        for (uint256 i = 0; i < _farmerIds; i++) {
            itemSpecificFarmers[i] = globalFarmers[_farmerIds[i]];
        }
        item = Item(itemSpecificFarmers, _name, _date, "0", "0");
    }
}

This is the farm's contract

pragma solidity >=0.5.0 <0.6.0;

contract Farm {
    struct Farmer {
        string name;
        string item;
    }
}

1 Answer 1

1

I changed up the code a bit and it compiles fine now:

pragma solidity >=0.5.0 <0.6.0;

contract Test {
    struct Farmer {
        string name;
        string item;
    }

    struct Item {
        Farmer[] farmers;
        string manufacturerName;
        string manufacturerDate;
        string distributorDataDate;
        string distributorName;
    }

    Farmer[] public globalFarmers;

    function addFarmData(string memory _name, string memory _item) public {
        globalFarmers.push(Farmer(_name, _item));
    }

    function addManufacturerData(
        uint256[] memory _farmerIds,
        string memory _name,
        string memory _date
    ) public {
        Farmer[] memory itemSpecificFarmers = new Farmer[](_farmerIds.length - 1);
        for (uint256 i = 0; i < _farmerIds.length; i++) {
            itemSpecificFarmers[i] = globalFarmers[_farmerIds[i]];
        }
        Item memory item = Item(itemSpecificFarmers, _name, _date, "0", "0");
    }
}

Does that solve your problem?

2
  • Sorry, @ikjiong, I forgot to mention one thing in my code I already have an item declaration as a state variable, and for now, I'm trying to add the data using the declared variable. See my code for the edits, also can you please explain what the reason for the problem is?
    – Mehrose
    Sep 7, 2022 at 10:43
  • I would call the variable item instead (structure is called Item). Apart from defining the variable, I just looped until _farmerIds.length instead. Sep 7, 2022 at 10:51

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.