'What is this solidity variable type?
I'm following a 16hr solidity course and I'm stuck. I can't figure out what SimpleStorage is. Is it a class, is it the imported document, I have no idea. Its used multiple times but in different ways and Im confused. I'm happy to share more information if this isn't enough to figure this out.
SimpleStorage[] public simpleStorageArray;
SimpleStorage simpleStorage = SimpleStorage(address(simpleStorageArray[_simpleStorageIndex])).store(_simpleStorageNumber);
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;
import "./SimpleStorage.sol";
contract StorageFactory is SimpleStorage {
SimpleStorage[] public simpleStorageArray;
function createSimpleStorageContract() public {
SimpleStorage simpleStorage = new SimpleStorage();
simpleStorageArray.push(simpleStorage);
}
function sfStore(uint256 _simpleStorageIndex, uint256 _simpleStorageNumber) public {
SimpleStorage simpleStorage = SimpleStorage(address(simpleStorageArray[_simpleStorageIndex])).store(_simpleStorageNumber);
}
Solution 1:[1]
SimpleStorage is a contract imported from import "./SimpleStorage.sol". Contracts in solidity are defined by contract keyword which can be thought of as being class. We deploy this contracts in a network and those deployed versions can be thought of as instances of class. If you look at any contract it has the definition of class. The main difference, in classes we create a new instance like this:
my_instance=MyClass() # in python
cont myInstance=new MyClass() // javascript
But in contract, we create instances when we deployed the contract. we cannot create contract instances the way how we create a class instance.
contract StorageFactory is SimpleStorage{}
this is inheritance meaning that we can access SimpleStorage state variables and functions inside here.
SimpleStorage[] public simpleStorageArray;
We are creating an array that stores SimpleStorage contract instances and we name this array simpleStorageArray.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
