0

I am giving the values to structure field through Remix IDE, then I am putting ‘0’ as the index of people array, then I click transact and then I click call, but Remix does not show me the value I gave as input to the structure fields (encircled in blue box). However, previously it worked: Understanding array of structure The code is:

    pragma solidity 0.5.4;
    contract MyContractOwnerAddr {
        uint256 public peopleCount = 0;
        mapping(uint => Person) public people;

        address owner;

        modifier onlyOwner() {
            require(msg.sender == owner);
            _;
        }

        struct Person {
            uint _id;
            string _firstName;
            string _lastName;
        }

        constructor() public {
            owner = msg.sender;
        }

        function addPerson(
            string memory _firstName,
            string memory _lastName)
            public
            onlyOwner
        {
            incrementCount();
            people[peopleCount] = Person(peopleCount, _firstName, _lastName);
        }

        function incrementCount() internal {
            peopleCount += 1;
        }
    }

Some body please guide me.

Zulfi. Problem with retrieving textbox values

2
  • The code from your original post is still working in remix for me. Have you added or changed any code?If so please post. Have you changed the compiler version? – Steven V Jan 20 '20 at 21:00
  • @Steven V I have added the code, kindly check it if possible. – zak100 Jan 21 '20 at 1:16
1

The increment people count is added before adding the person to the mapping. So nothing is added to the people[ 1 ] spot in the mapping. As is, the first spot in the mapping that gets filled is people[ 0 ]. Move incrementCount() to after adding the person to the mapping.

enter image description here

2
  • Thanks, it worked after several tries. Why you put 0 in quotations? But I tired without quotations. – zak100 Jan 21 '20 at 4:48
  • 1
    I only did because that’s what your screen shot had :) - normally I wouldn’t have. Seemed to work both ways for me. – Steven V Jan 21 '20 at 12:31

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.