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

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 answwersanswers 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.

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 answwers 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.

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.

added 187 characters in body
Source Link
MAB
  • 3
  • 2

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 answwers 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.

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

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 answwers 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.

Source Link
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