Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Converstion error from uint to int in Solidity

I have been trying to learn solidity.

Wrote a simple program as shown below

pragma solidity >=0.8.12 <0.9.0;

contract Test {
    int[] staticArray = [int(1), int(2), int(3), int(4), int(5)];

    function getStaticArray(int _pos) public view returns(int) {
        int ret = staticArray[_pos];
        return ret;
    }
}

But giving following conversation error for some reason:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

TypeError: Type int256 is not implicitly convertible to expected type
uint256.

–> first.sol:13:31:

|

13 | int ret = staticArray[_pos];

| ^^^^

Which I am not sure why this error is thrown. Can anyone help me with it?

>Solution :

You have to use uint instead of int when you are using index in an array.

function getStaticArray(uint _pos) public view returns(int) {
    int ret = staticArray[_pos];
    return ret;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading