0

I am new to Solidity, and I have a small problem. For exemple I have these 2 contracts

contract Dog {
   string name;
   constructor(string _name){
       name = _name;
   }
}

contract Human {
    Dog[] dogs;
    uint currDogs = 0;

    function addDog() public {
        dogs[currDogs] = new Dog("test");
        //I also tried dogs.push(new Dog("test"));
        currDogs++;
    }
}

everytime I try adding a dog to the array it is not working and I can't find a solution

EDIT: thanks guys, all the answers are correct, even my code is working fine, I found out that I was re deploying the contract everytime, that's why my changes are not getting saved.

2
  • I didn't have a problem with the push method but I had to set a fixed side array for the other way
    – sola24
    Commented Jun 9, 2022 at 20:59
  • What do you mean by "it's not working" ? Commented Jun 10, 2022 at 6:07

3 Answers 3

1

What version of solidity are you using and what is the error you are getting?

I have tested your code and it works adding the data location "memory" to the parameter in the constructor of the first contract and using

dogs.push(new Dog("test"));

//SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.14;

contract Dog {
  string name;
  constructor(string memory _name){
       name = _name;
   }
}

contract Human {    
  Dog[] public dogs;
  uint public currDogs;

  function addDog() public {
    dogs.push(new Dog("test"));       
    currDogs++;
  }    
}

I'm not sure what you are trying to do but you could consider using a struct in the following way

//SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.14;

contract Human {
    struct Dog {
        string name;
    }

    Dog[] public dogs;
    uint256 public currDogs;

    function addDog() public {
        dogs.push(Dog('test'));
        currDogs++;
    }
}
1

I would have thought an array of structs would have been the natural choice here but assuming there's a reason you want an array of contracts I stuck with your example.

No issue pushing the contracts into a dynamic array, but needed to define a fixed size array to assign by element number. The element has to exist before it can be assigned. No problem assigning by element number once the element exists even with the dynamic array. e.g.

string[] bigDogs;
bigDogs.push("fido");
bigDogs.push("woof");
bigDogs[2] = ("bark");  // will error
bigDogs.push("");
bigDogs[2] = ("bark");  // ok now

pragma solidity ^0.8.14;

import "hardhat/console.sol";

contract Dog {
   string name;
   
   constructor(string memory _name){
       name = _name;
       console.log("conatract deployed with dog name", name);
   }
}

contract Human {
    Dog[] bigDogs;
    Dog[3] smlDogs;       // or Dog[] smlDogs = new Dog[](3);
    uint currDogs = 0;

    function addSmlDogs() public {
        smlDogs[0] = new Dog("pooch");
        smlDogs[1] = new Dog("pinky");
        smlDogs[2] = new Dog("puff");
    }

    function addBigDogs() public {
        bigDogs.push(new Dog("fido"));
        bigDogs.push(new Dog("woof"));
        bigDogs.push(new Dog("bark"));
    }

    function showDogs() external view {
        for(uint i; i < smlDogs.length; ++i ) {
            console.log(address(smlDogs[i]));
        }
        for(uint i; i < bigDogs.length; ++i ) {
            console.log(address(bigDogs[i]));
        }  
    }
}

output

[vm]from: 0x5B3...eddC4to: Human.addBigDogs() 0x7FD...9Bd66value: 0 weidata: 0xa06...68f30logs: 0hash: 0x254...18c37
console.log:
 conatract deployed with dog name  fido
 conatract deployed with dog name  woof
 conatract deployed with dog name  bark
 
transact to Human.addSmlDogs pending ... 
[vm]from: 0x5B3...eddC4to: Human.addSmlDogs() 0x7FD...9Bd66value: 0 weidata: 0x9cb...a3d07logs: 0hash: 0x9c3...39212
console.log:
 conatract deployed with dog name  pooch
 conatract deployed with dog name  pinky
 conatract deployed with dog name  puff
 
call to Human.showDogs
CALL
[call]from: 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4to: Human.showDogs()data: 0xdbd...5503b
console.log:
 0xFE6310B4745c4a9b9977A5b06B2e3834360b3Cfd
 0x61367B4487FA6364FAc15965068d9629A2442efE
 0x70D6ED8b901Cc7c067B6B054b287b3929aF6A0F4
 0x5416e367FbA90B0f3E05b4e20Abef8F344B240c2
 0x0484d676FCFd6350C28a6Fc58F2D92531D90ea13
 0x2c31f122D5FA5A44cE717fd6E8718F7F7e040Bf2
0

Do it in steps:

function addDog() public {
        Dog newDog = new Dog("test");
        dogs.push(newDog);
    }
1
  • Hello. If that answers the question, could yo uexplain why, in order to avoid incomprehensible code only answers. Commented Jun 10, 2022 at 6:08

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.