I am totally new to solidity and not sure how different data types are used.
I am writing a code that handles an array of smart contracts, read and store values in them etc. In one function (storevalueincontract) I want to take an index (of an array consisting of smart contracts) and a value, and store that value via the function (store) in that contract. However, when I compile the code, I get the following error message:
TypeError: Integer constant expected.
referring to the following line:
simplestorage con = simplestorage[_index];
Here is the code:
// SPDX-License-Identifier: MIT
pragma solidity 0.8.8;
import "./simplestorage.sol";
contract Storage {
simplestorage[] public cons;
function createsimplestorage() public{
simplestorage cont=new simplestorage();
cons.push(cont);
}
function storevalueincontract(uint _index, uint _value) public{
simplestorage con = simplestorage[_index];
con.store(_value);
}
function viewvaluestoredinacontract(uint _index) public view returns(uint){
simplestorage con= simplestorage(simplestorage[_index]);
return con.retrieve();
}
Here is also the code for the smart contract named "simplestorage" in case it is needed:
// SPDX-License-Identifier: MIT
pragma solidity 0.8.8; // my first line in solidity
contract simplestorage{
uint256 public favNum;
people public father= people({favNum:2,name:"Faw"});
people[] public family;
mapping(string => uint256) public name2FavNum;
struct people{ //new type for people's name and favourite number
uint256 favNum;
string name;
}
function retrieve() public view returns(uint){
return favNum;
}
function store(uint256 _favNum) public {
favNum=_favNum;
}
function addperson(uint256 _num, string memory _name) public{
people memory newperson = people({favNum:_num,name:_name});
family.push(newperson);
name2FavNum [_name]=_num;
}
}
What could be the problem and how to solve it? I would appreciate any kind of help! thanks alot.
simplestorage
? Is it a struct name, or an array, or a function, or a contract. Don't use the same name multiple times.