1

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.

2
  • What is simplestorage? Is it a struct name, or an array, or a function, or a contract. Don't use the same name multiple times.
    – Ismael
    Nov 26, 2022 at 6:48
  • @Ismael it is a smart contract. I have added its code to the question. If you could have a look, I would be thankful.
    – FAW
    Nov 26, 2022 at 9:19

1 Answer 1

0

The array is named cons but you used simplestorage instead.

If you replace by the correct name it compiles.

function storevalueincontract(uint _index, uint _value) public{
    simplestorage con = cons[_index];
    con.store(_value);
}


function viewvaluestoredinacontract(uint _index) public view returns(uint){
    simplestorage con= simplestorage(cons[_index]);
    return con.retrieve();    
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.