Please explain the idea of an array of contracts and the implementation of "sfStore" function #3412
-
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "./SimpleStorage.sol";
contract StorageFactory {
SimpleStorage[] public simpleStorageArray;
function createSimpleStorageContract() public {
SimpleStorage simpleStorage = new SimpleStorage();
simpleStorageArray.push(simpleStorage);
}
function sfStore(uint256 _simpleStorageIndex, uint256 _simpleStorageNumber) public {
// Address
// ABI
// SimpleStorage(address(simpleStorageArray[_simpleStorageIndex])).store(_simpleStorageNumber);
// Do explain this line of code in brief
simpleStorageArray[_simpleStorageIndex].store(_simpleStorageNumber);
}
function sfGet(uint256 _simpleStorageIndex) public view returns (uint256) {
// return SimpleStorage(address(simpleStorageArray[_simpleStorageIndex])).retrieve();
return simpleStorageArray[_simpleStorageIndex].retrieve();
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
@kprabhasreddy What you did not understand? |
Beta Was this translation helpful? Give feedback.
-
In this contract you wan to create as many The new contracts are added to What happens if you have 5 |
Beta Was this translation helpful? Give feedback.
In this contract you wan to create as many
SimpleStorage
contracts you want, every time a newSimpleStorage
is created it will be store insimpleStorageArray
to be access whenever you want via its index.The new contracts are added to
simpleStorageArray
using thepush()
method. This method allows you to add new elements to an array.What happens if you have 5
SimpleStorage
and you want to add numbers to them?sfStore
takes two parameters: the index of a contract and the number to store, with these parameters you could add a number to any contract that is already stored insimpleStorageArray
. This also apply tosfGet
, if you pass an index to this function it will show you what number is s…