1

solidity 0.4.17

I have an array of int128 elements, I chose in128 because I want to check for -1.

int128[] elements;
int128 numberOfElements;//I increment this as I add elements

function hasElement(int128 element) returns (bool) {
    for ( int128 i = 0; i < numberOfElements; i++ ) {
        if (elements[i] == element) {
            return true;
        }
    }
    return false;
}

but I get the error:

TypeError: Type int128 is not implicitly convertible to expected type uint256 on the i in elements[i].

Yet i is int128 and so are the contents of elements.

  • I am not pretty sure about the error. But why not use uint for noOfElements ? – Prashant Prabhakar Singh Nov 14 '17 at 12:42
  • Yes! That worked. – Phillip Gibb Nov 14 '17 at 12:44
  • So I changed it to use uint for numberOfElements and the i in the for loop – Phillip Gibb Nov 14 '17 at 12:46
  • Haha.. I write the answer the answer for you yesterday, but didn't click thePost Anser button . LOL – Prashant Prabhakar Singh Nov 15 '17 at 4:07
  • You must index an array with uint256. Otherwise Solidity will attempt to convert whatever you put in the index to uint256, hence the error. – libertylocked Nov 15 '17 at 4:56
0

I am not pretty sure about why you can't use int128. But you cal solve your problem by using uint datatype for noOfElements. Keep the array as int128 (as you need this), but update the datatype for noOfElements.

int128[] elements;
uint numberOfElements;//I increment this as I add elements

function hasElement(int128 element) returns (bool) {
    for ( uint i = 0; i < numberOfElements; i++ ) {
        if (elements[i] == element) {
            return true;
        }
    }
    return false;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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