Skip to main content
3 of 3
deleted 1 character in body
MAB
  • 3
  • 2

Pushing a contract to an array in another contract

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.

MAB
  • 3
  • 2